Javascript obtain the address bar (URL) parameter information

1. Get all of the parameter information

function (url) {// retrieves all parameters from a given address or a default address bar
was whose = {},
hash;
var hashes = null;

if (!url)
url = window.location.search;

hashes = url.slice(url.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');

if (!!hash && hash[0].length > 0)
vars [hash [0]] = hash [1];
}
Return whose;
}

2. Obtain information specified parameters

function (name) {// Get the specified value of the parameter from the browser address bar
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(window.location.search);
return results === null ? '' : results[1].replace(/\+/g, ' ');
}

Guess you like

Origin www.cnblogs.com/john-paul/p/11271627.html