-
-
Save ccwq/ee94b78fa8e9b78af717a431ba8457dd to your computer and use it in GitHub Desktop.
url相关
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @function: 通过a标签解析url标签 | |
* @param:url url参数是字符串,解析的目标 | |
通过IE6-9 chrome Firefox测试 | |
* | |
*/ | |
function parseURL(url) { | |
//创建一个a标签 | |
var ac = arguments.callee; | |
if(!ac.a) { | |
ac.a = document.createElement('a'); | |
ac.div = document.createElement('div'); | |
} | |
var a = ac.a; | |
var div = ac.div; | |
//将url赋值给标签的href属性。 | |
a.href = url; | |
if((a.parentNode || a.parentNode) == div){ | |
div.removeChild(a); | |
} | |
div.appendChild(a); | |
return { | |
source: url, | |
protocol: a.protocol.replace(':',''), //协议 | |
host: a.hostname, //主机名称 | |
port: a.port, //端口 | |
query: a.search, //查询字符串 | |
params: (function(){ //查询参数 | |
var ret = {}, | |
seg = a.search.replace(/^\?/,'').split('&'), | |
len = seg.length, i = 0, s; | |
for (;i<len;i++) { | |
if (!seg[i]) { continue; } | |
s = seg[i].split('='); | |
ret[s[0]] = s[1]; | |
} | |
return ret; | |
})(), | |
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('/'), //路径片段 | |
fullpath:a.href, | |
href:a.href, | |
toString:function(){ | |
return a.href; | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var urlParas = function(url) { | |
url = url||location.href; | |
var urlObject = {}; | |
if (/\?/.test(url)) { | |
var urlString = url.substring(url.indexOf("?")+1); | |
var urlArray = urlString.split("&"); | |
for (var i=0, len=urlArray.length; i<len; i++) { | |
var urlItem = urlArray[i]; | |
var item = urlItem.split("="); | |
urlObject[item[0]] = item[1]; | |
} | |
} | |
return urlObject; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
谢谢