This question already has answers here:
Updating existing URL querystring values with jQuery
(12 answers)
Closed 4 years ago.
I need to change the value of query parameter present in the given URL using jQuery.
There are 2 possible URL's like,
www.domain.com/?id=10&name=xxx
and
www.domain.com/?name=xxx&id=10
I need to change the value of parameter id in this URL into something like www.domain.com/?id=15&name=xxx. Regex seems to be the solution for this, but it looks confusing to me.
Possible solution:
Select the string between "id=" and "&" (or) string from "id=" to the end of string and replace it with desired value 15.
Does anyone have solution for this or any other better solution?
Thanks in advance!
var url="www.domain.com/?id=10&name=xxx";
changeUrl("id",15);
changeUrl("name","sumesh");
function changeUrl(key,value){
var patt = new RegExp(key+"=[a-zA-Z0-9]+");
var matches = patt.exec(url);
var id2 = matches[0];
url = url.replace(id2, key+'='+value);
console.log(url);
}
Use RegExp to achieve this.
Why don't you use simple replace?
var newUrl = location.href.replace("id="+10, "id="+15);
I found this solution from an answer to the question
Change url query string value using jQuery (marked as duplicate).
Related
This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 9 months ago.
An API returns a string of text that looks like this (xxx used for security):
{"xxx":{"xxx":{"xxx":{"xxx":{"results":[{"latest.GigabytesIngested":12641.824682336}]}}}}}
If I do this:
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]);
I get this, which is fine:
{ 'latest.GigabytesIngested': 12641.82487968 }
My problem is I only want to grab the number. The below attempt doesn't work, maybe because there's a dot in the key name, or maybe because I'm just doing it wrong?
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0].latest.GigabytesIngested);
#derpirscher answered correctly in a comment:
console.log(JSON.parse(body).data.actor.account.nrql.results[0]['latest.GigabytesIngested']);
Yes, the period in the key is the problem. You need to use an alternate way to reference the key.
console.log(JSON.parse(body).xxx.xxx.xxx.xxx.results[0]["latest.GigabytesIngested"]);
or
var result = JSON.parse(body).xxx.xxx.xxx.xxx.results[0];
var lgi = result["latest.GigabytesIngested"];
console.log(lgi);
This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 6 years ago.
Here is my html tag with href
<a href='components/home/singleActivity.html?view="search"'>
....
</a>
I want to send a string parameter, is it the correct way ?
how to retrieve that parameter in javascript ? and how to send 2 parameters for the same href ?
It should be without quotes components/home/singleActivity.html?view=search to send two parameters join them with &
components/home/singleActivity.html?foo=bar&baz=quux
to read them in javascript use this code:
var params = {};
location.search.slice(1).split("&").forEach(function(pair) {
pair = pair.split("=");
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
});
Try
<a href='components/home/singleActivity.html?view=Search&test2=New'>
MyLink
</a>
In order to send the parameters in the URL you donot need to enclose the value inside quote('') or doublequotes(""). You just need to send the values (or the multiple values) as shown below..
components/home/singleActivity.html?view=search&secondvalue=anythingthatyouthinkof
Also keep in mind that you need to keep track of the urlencoding.
And for retrieving the parameters this thread explains it in great detail.
To send multiple values you can use ?view=search&key2=value2&key3=value3 and so on.
Moreover to access these parameters you can access the URL using window.location.
Something like
var params = window.location.split('?')[1];
This will give you everything after the ? in the URL.
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 6 years ago.
My links are like that:
index.php
index.php?do=that
index.php?do=this
And my current javascript code is like that:
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
console.log(url);
$('.nav-link').each(function(){
if(urlRegExp.test(this.href.replace(/\/$/,''))){
$(this).addClass('active');
}
});
});
So with this code even if I'm at index.php?do=this console log says to me /path/index.php
I think I need make changes on var url = window.location.pathname line but I couldn't.
How should I improve my code? There are bunch of similiar questions here in SO, but not the same one.
I appreciate any help.
In PHP you can get your URL parameters from GET array.
$_GET['do'] or try to print $_GET. This array will print all the URL parameters.
Hope this helps!
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get query string values in JavaScript
the link is
http://nycs00058260/sites/usitp/_layouts/CreateWebPage_DingDing.aspx?List={74AB081E-59FB-45A5-876D-284607DA03C6}&RootFolder=%3b&Text=%27MDNSO%27
how can I parse the parameter "Text"(at the end) from the url using javascript?
anyone can help me?
URI.js is a small library that makes working with URLs nice and easy :)
var url = new URI("http://nycs00058260/sites/usitp/_layouts/CreateWebPage_DingDing.aspx?List={74AB081E-59FB-45A5-876D-284607DA03C6}&RootFolder=%3b&Text=%27MDNSO%27");
var queryMap = URI.parseQuery(url.query());
var text = queryMap["Text"];
console.log("Text: ", text);
You can combine window.location.search : https://developer.mozilla.org/en-US/docs/DOM/window.location
with indexOf : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/indexOf
and substring: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/substring
This question already has answers here:
Replace function not replacing [duplicate]
(2 answers)
Closed 8 years ago.
I have written a simple code in a .js file (in Zend framework, but I dont think it matters)
var location = "localhost:8080/mymodule/id/1#";
location.replace(/\#/g, "");
alert(location.valueOf());
return;
but I dont know why I can not see the result I want.
I get my page url and want to omit all number signs appears in it. but the code above does nothing.
please help
location is a bad name to use for a variable since it collides with the window.location variable used for the actual browser page location.
If you change location to loc in your above code, and then also add loc = in front of the loc.replace() call (since replace() doesn't modify the input, but instead returns the new version), your code works.
replace will not change the value of the original string, you need to assign the result to a new variable -
var newString = location.replace(/#/g, "");
alert(newString);
Demo - http://jsfiddle.net/5H5uZ/
It can be done in one line. This is the result you look for?
alert("localhost:8080/mymodule/id/1#".replace(/#/g,''));
//=> alerts 'localhost:8080/mymodule/id/1'