views:

40

answers:

2

I have the following:

$(document).ready(function() {
window.location.href='http://target.SchoolID/set?text=';
});

So if someone comes to a page with the above mentioned code using a url like:

Somepage.php?id=abc123

I want the text variable in the ready function to read: abc123

Any ideas?

+1  A: 

you don't need jQuery. you can do this with plain JS

function getParameterByName( name ) //courtesy Artem
{
  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, " "));
}

and then you can just get the querystring value like this:

alert(getParameterByName('text'));

also look here for other ways and plugins: http://stackoverflow.com/questions/901115/get-querystring-with-jquery

Moin Zaman
+2  A: 

check out the answer to this questions: http://stackoverflow.com/questions/901115/get-querystring-with-jquery

that'll give you the value of the id parameter from the query string.

then you can just do something like:

$(document).ready(function() {
    var theId = getParameterByName( id)
    var newPath = 'http://target.SchoolID/set?text=' + theId
    window.location.href=newPath;
});
Patricia