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
getParameterByNamefunction (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=anothervalueNow you can do this:
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:I do suggest you use the key/value method though, in case you want to pass on more variables in the future.