jQuery: Using window url parameter

Let’s say we have passed a parameter to URL: www.mysite.com/?page=test

To collect the data form parameter we can use this function:

function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};

use of this code:

getUrlParameter('page');

or

if (getUrlParameter('page') == 'test'){
// do something
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.