Obtain a PHP Get Varable via Javascript Function

I have a url like:

function getHash(){
	var hash = window.location.hash.match( /#([0-9a-zA-Z_]+)/ );
	return hash ? hash[ 1 ] : undefined;
}

This returns abc213

I’m wanting to change the url to: http://domain.com/?id=abc213/

How would I modify the function to search for ?id= instead of #

One way to get the id param from the query string would be to change the first line of your function to this:

var hash = window.location.search.match( /\?id=([0-9a-zA-Z_]+)/ );

Although as you can have multiple parameters in a query string, it might be more useful to have a function that can return the value of any parameter when given the key:

function getQueryVariable(variable) {
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

(taken from http://css-tricks.com/snippets/javascript/get-url-variables/)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.