Get URL Parameter Using Javascript

It is sometimes required to get the query parameters of an URL in JavaScript. So following example demonstrates how to get parameter value from a URL.

Source Code

<!DOCTYPE html>
<h4 id='div1'></h4>
function getUrlParameter( name ) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
	  document.getElementById('div1').innerHTML = 'No Parameter passed';
	  document.getElementById('div1').innerHTML = 'You have passed val=' + results[1];

No comments yet.

Leave a Reply