How to get a query string out of javascript

This function does an alert on what you request… it’s a great little function picked it up from

activsoftware.com

function getQueryVariable(variable) {

var query = window.location.search.substring(1);

var vars = query.split(“&”);

for (var i=0;i<vars.length;i++) {

var pair = vars[i].split(“=”);

if (pair[0] == variable) {

return pair[1];

}

}

alert(‘Query Variable ‘ + variable + ‘ not found’);

}

Call a page, page.html?x=test

alert( getQueryVariable(“x”) );

Leave a comment