Get the URL parameter with JavaScript

From The IT Community
Jump to: navigation, search




If you need to get one or more URL parameter in JavaScript, you can use the following function:

function get_url_parameter( name )
{
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 results[1];
}	

Call this function as follow:

playername=get_url_param('player');

This will fill the variable playername with the URL parameter player.



Was this article helpful? Then please donate to keep The IT Community alive...

If you found this article helpful please share it, comment and help others by writing your own article.






Translate this page:




Articles found in the same category:
(max. 20 shown)