Suppose that I have a URL like the following:
http://localhost:8000/intranet/users/view?user_id=8823
Now, all I want to do is to get the value of the URL using JavaScript and parse it, taking the user_id value (which is 8823 in this case) and sending that value through an iframe.
How can I do this?
try this code
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
i found it at How can I get query string values in JavaScript?
Try using window.location.href or document.URL
Do this:
var matches = document.location.search.match( /user_id=(\d+)/ );
if ( matches != null )
{
alert( matches[ 1 ] );
}
matches[ 1 ] will contain the user ID.
document.location.search contains the query string (all of the parameters which follow the '?' including the '?').
var test = "http://localhost:8000/intranet/users/view?user_id=8823";
//var url = document.URL;
var url = test.split("=");
var urlID = url[url.length-1];
document.write(urlID);
window.frames["myIframe"].yourMethod(urlID);
Related
response.sendRedirect("http://localhost:8080/cse/welcome.html?first=fname&last=lname&dname=dept&mname=mobno");
How to extract the first,last,dname,mname parameters from the Url and I want to use those extracted values in my redirected html document(welcmoe.html). How can I achieve this?
If you wanted client side using JS, you could do the following.
//Create an array from query strings
//such as ['first=fname', 'last=lname', ...]
var querystring = location.search.substring(1).split("&");
var props = {};
//Loop thru the array
querystring.forEach(function(item) {
//split each item
var item = item.split("=");
//set the first part as key and last/second part as value
props[item.shift()] = item.pop();
});
alert ("first: " + props["first"]);
alert ("last: " + props["last"]);
//and so on...
The four variables from the java servlet can be hardcoded and passed as a string like this
StringBuilder url =new StringBuilder();
url.append("http://localhost:8080/cse/welcome.html");
url.append("?first="+fname);
url.append("&last="+lname);
url.append("&dname="+dept);
url.append("&mname="+mobno);
String URL=url.toString();
String urlEncoded=response.encodeRedirectURL(URL);
response.sendRedirect(urlEncoded);
Then these variables can be obtained and called in the javascript in this way
<script>
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var fname = getParameterByName('first');
var lname=getParameterByName('last');
var dept=getParameterByName('dname');
var mobno=getParameterByName('mname');
</script>
I have a problem , my original URL looks like this:
test.com/?manufacturer=0&body-style=0&min-price=270%2C000&max-price=780%2C000
As you can see, the min-price and max-price values in the query string is not correct due to the comma that is passed to the URL. It should be in their respective integer value like min-price=270000 and max-price=780000.
I need to convert the query string values of min-max and max-price using jQuery. I currently do not how to do this actually. But I have codes to get them from the URL and then convert them to the correct value. I just don't know how to implement them back to the URL (as new URL) using jQuery. These are my existing codes:
//Function to get value of parameter in query string
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
//Function to remove commas and convert to number
function convert_to_pure_number(x) {
//Remove commas
var x_withoutcommas=x.replace(/,/g,'');
//Convert to plain number
var y =parseInt( x_withoutcommas ,10);
return y;
}
var min_price_original=getParameterByName('min-price');
var max_price_original=getParameterByName('max-price');
var min_price_converted=convert_to_pure_number(min_price_original);
var max_price_converted=convert_to_pure_number(max_price_original);
Any suggestions how will I continue the above code with the additional code to put them back to the URL posted? Thanks for any help.
UPDATE
This is the process:
Form will be posted to the server--> URL will contain commas --> My new code will remove the comma --> In the query string value, correct value will be used.
Cheers.
use replace function like this :
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var min_price_original=getParameterByName('min-price').replace('%2C','');
var max_price_original=getParameterByName('max-price').replace('%2C','');
I have got a current URL looking like this:
http://example?variables1=xxxx&example&variables2=yyyyy
I want to use the variables1 and variables2 to create a new URL and open this new URL:
http://example?variables3=variables1&example&variables4=variables2
I hope someone can help me with this :)
You will need to parse the desired query parameters from the first URL and use string addition to create the second URL.
You can fetch a specific query parameter from the URL using this code. If you were using that, you could get variables1 and variables2 like this:
var variables1 = getParameterByName("variables1");
var variables2 = getParameterByName("variables2");
You could then use those to construct your new URL.
newURL = "http://example.com/?variables1=" +
encodeURIComponent(variables1) +
"&someOtherStuff=foo&variables2=" +
encodeURIComponent(variables2);
Because I don't fully understand what needs to change, here's my best attempt*, using a mashup of other answers and resources online.
// the original url
// will most likely be window.location.href
var original = "http://example?variables1=xxxx&example&variables2=yyyyy";
// the function to pull vals from the URL
var getParameterByName = function(name, uri) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(uri);
if(results == null) return "";
else return decodeURIComponent(results[1].replace(/\+/g, " "));
};
// so, to get the vals from the URL
var variables1 = getParameterByName('variables1', original); // xxxxx
var variables2 = getParameterByName('variables2', original); // yyyyy
// then to construct the new URL
var newURL = "http://" + window.location.host;
newURL += "?" + "variables3=" + variables1;
newURL += "&example&"; // I don't know what this is ...
newURL += "variables4=" + variables2;
// the value should be something along the lines of
// http://example?variables3=xxxx&example&variables4=yyyy
*All of which is untested.
I have the following url
http://www.test.info/link/?url=http://www.site2.com
How do I get the value of the url parameter with regular expressions in javascript?
Thanks
function extractUrlValue(key, url)
{
if (typeof(url) === 'undefined')
url = window.location.href;
var match = url.match('[?&]' + key + '=([^&]+)');
return match ? match[1] : null;
}
If you're trying to match 'url' from a page the visitor is currently on you would use the method like this:
var value = extractUrlValue('url');
Otherwise you can pass a custom url, e.g.
var value = extractUrlValue('url', 'http://www.test.info/link/?url=http://www.site2.com
Check out http://rubular.com to test regex:
url.match(/url=([^&]+)/)[1]
Might want to check this: http://snipplr.com/view/799/get-url-variables/ (works without regEx)
This one does use regEx: http://www.netlobo.com/url_query_string_javascript.html
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
var param = gup( 'var' );
Well, firstly, is that the whole query string? If so, all you need to do is split on the =:
url.split('=')[1];
Otherwise, you might want to use Jordan's Regex.
On the answer code:
function extractUrlValue(key, url)
{
if (typeof(url) === 'undefined')
url = window.location.href;
var match = url.match('[?&]' + key + '=([^&#]+)');
return match ? match[1] : null;
}
If key is a last parameter in URL and after that there is an anchor - it enter code herewill return value#anchor as a value. # in regexp '[?&]' + key + '=([^&#]+)' will prevent that.
I would like to insert a parameter value (AS A NUMBER) found the url into my javascript code.. So basically i have the following url www.rene-zamm.com/mp-dthanks.asp?gglid=123123123
Now i have the following javascript code in the same page and i want that number in the url to be visible also in the javascript code where i have written querydata..:
<script type="text/javascript">
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
try {
var querydata = gup("gglid");
var gwoTracker=_gat._getTracker("UA-1639156-3");
gwoTracker._trackPageview("/" + **querydata** + "/goal");
alert(querydata);
}catch(err){}
</script>
For some reason the querydata is showing as text and not as number.. please help
If you're sure that querydata is going to be a number, just use parseInt(querydata) and it will return a number, integer to be exact