“站在伟人的肩膀上”
灵感泉源:
https://segmentfault.com/a/1190000004601319
和
http://mp.weixin.qq.com/mp/ad_biz_info?__biz=MzAwMjU3OTY5NQ==#wechat_webview_type=2&wechat_redirect
上面一个地点,能够让你学到怎样剖析一个url地点,从中剖析出每个参数内容
第二个链接的题目就是参数中的键值对涌现了==号,这是base64信息,假如再用=切割,就会出题目,这里就改版了一下
String.prototype.parseURL = function(){
var url =this.toString()
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':', ''),
host: a.hostname,
port: a.port,
query: a.search,
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
hash: a.hash.replace('#', ''),
path: a.pathname.replace(/^([^\/])/, '/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
segments: a.pathname.replace(/^\//, '').split('/'),
params: (function() {
var ret = {};
var seg = a.search.replace(/^\?/, '').split('&').filter(function(v,i){
if (v!==''&&v.indexOf('=')) {
return true;
}
});
seg.forEach( function(element, index) {
var idx = element.indexOf('=');
var key = element.substring(0, idx);
var val = element.substring(idx+1);
ret[key] = val;
});
return ret;
})()
};
}
挪用要领:
location.href.parseURL();
'http://a.com:8888/a/b.html?c=1&0=0&d===&=1'.parseURL();
//上面有意把参数写的很乱,为了测试,假如上面你的浏览器报错,申明版本较低,能够以下写法
('http://a.com:8888/a/b.html?c=1&0=0&d===&=1').parseURL();