在网上找的这两种处理方式,但不理解函数的逻辑和正则部分,另这两种方法的不同和哪个在项目中使用性能会更高呢?希望有懂得大神能指点迷津!谢谢?!!
http://www.cnblogs.com/season...
方法一:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search); return results == null ? "": decodeURIComponent(results[1]);
}console.log(getParameterByName("param")) //yes方法二:
var urlParams;
(window.onpopstate = function() { var match, pl = /\+/g, search = /([^&=]+)=?([^&]*)/g, decode = function(s) {
return decodeURIComponent(s.replace(pl, " "));
}, query = window.location.search.substring(1);
urlParams = {}; while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
//urlParams的结果
urlParams = {
param: "yes",
article: "1"
}
console.log(urlParams["param"]); // -> "yes"
console.log("article" in urlParams); // -> true
斯蒂芬大帝
德玛西亚99
随时随地看视频慕课网APP
相关分类