I am 'sending' some data in a url:
foo.htm?mydata
From searching around I know that I must use something like:
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
But I am a bit perplexed by this. Could someone help me out in deciphering this at all I simply want the end result to be placing mydata
in a var, eg:
var newvar = mydata
Thanks heaps in advance!
The getParameterByName
function (I assume you got it from here ), is written to retrieve a query string value. This means, you can access data from the URL when it looks like this:
yourdomain.com/index.html?key=value&anotherkey=anothervalue
Now you can do this:
var firstKey = getParameterByName("key");
var secondKey = getParameterByName("anotherkey");
As described in your question, you don't have key/value pairs. If this is the case, and you only need the part after the ?
, simply use the following by using the split method:
var newvar = document.URL.split("?")[1];
I do suggest you use the key/value method though, in case you want to pass on more variables in the future.
Maybe this library is interesting if you are in need of a lot of uri parsing:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.