Membership Membership:
Latest New User Latest: vpros
Past 24 Hours Past 24 Hours: 0
Prev. 24 Hours Prev. 24 Hours: 0
User Count Overall: 137

People Online People Online:
Visitors Visitors: 4
Members Members: 0
Total Total: 4

NakedMCSE Computer Tech Support Forums

So you have passed all of the exams and you are now out in the real world of windows servers, linux servers and freebsd servers faced with a tech support problem you have never seen before...

Find free online tech support in the forums below, respecting that advice is free, but work beyond advice is not...

The General forum is provided for off topic discussion.

You need a user account to post - please Register or Login.

Online Tech Support Forums
  Forum  Discussions  General  Getting URL parameters using Javascript
Previous Previous
Next Next
New Post 9/15/2011 5:01 PM
119 posts
10th Level Poster

Getting URL parameters using Javascript 

Most server based languages like PHP and ASP provide simple ways to get to parameters on the URL line.

Javascript does not.  You need to custom write a function to use regular expressions to extract them from window.location.href.

This function will do that:
function getparam( paramname )
  paramname = paramname.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");

  var regexbuild = "[\\?&]"+paramname+"=([^&#]*)";
  var regex = new RegExp( regexbuild );
  var results = regex.exec( window.location.href );

  if( results == null )
    return "";
    return results[1];

You can then extract the parameters using the following type of code:

var natscode = getparam('nats');

Where this would extract the parameter nats to natscode.


Previous Previous
Next Next
  Forum  Discussions  General  Getting URL parameters using Javascript
Send me One Million FREE Guaranteed Visitors