I am trying to get a variable from a URL but it just keep returning null. I have tried to do this so many ways and read multiple tutorials but nothing seems to work. Below is the url.
http://example.com/page2.html?user%20=admin
and the javascript code I am using to get the variable
var getQueryString = function ( field, url ) {
var href = url ? url : window.location.href;
var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
var string = reg.exec(href);
return string ? string[1] : null;
};
var user = getQueryString('user');
document.write (user);