jQuery获取浏览器URL链接的值
代码:
方法一:
$.extend({ getUrlVars: function () { var vars = [], hash; var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); for (var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); vars.push(hash[0]); vars[hash[0]] = hash[1]; } return vars; }, getUrlVar: function (name) { return $.getUrlVars()[name]; } });
用法:
浏览器的url: http://域名/index.html?OrderCode=20151216173232283
获取OrderCode的值:
var result=$.getUrlVar("OrderCode")
方法二:
/** * 获取url中的参数,支持 /key/value/ 或 ?key1=value1&key2=value2 形式 * @param name * @returns {*} */ function getQueryString(name) { var reg = new RegExp('(?:(?:&|\\?)' + name + '=([^&]*))|(?:/' + name + '/([^/]*))', 'i'); var r = window.location.href.match(reg); if (r != null) return decodeURI(r[1] || r[2]); return null; }
用法:
URL:http://localhost:1353/Test.htm?name=张三
获取name的值
var result= getQueryString("name");