Javascript function to get URL query string parameters

/ Published in: JavaScript
Save to your folder(s)

Here is a small function [[lobo235]( "Javascript Tool-tips")] wrote that will parse the window.location.href value and return the value for the parameter you specify. It does this using javascript's built in regular expressions.

Copy this code and paste it in your HTML
  1. function gup( name )
  2. {
  3. name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  4. var regexS = "[\\?&]"+name+"=([^&#]*)";
  5. var regex = new RegExp( regexS );
  6. var results = regex.exec( window.location.href );
  7. if( results == null )
  8. return "";
  9. else
  10. return results[1];
  11. }


Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.