Last active
January 13, 2017 15:19
-
-
Save vace/fd2fc67be0adb429f12d3c0d21d5047e 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 parseUrlParam(url){ | |
var match, | |
urlParams = {}, | |
pl = /\+/g, // Regex for replacing addition symbol with a space | |
search = /([^&=]+)=?([^&]*)/g, | |
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); } | |
var query = url || location.href | |
var qus = query.indexOf('?') | |
if(qus !== -1){ | |
query = query.substring(qus + 1) | |
} | |
var sup = query.indexOf('#') | |
if(sup !== -1){ | |
query = query.substring(0,sup) | |
} | |
while (match = search.exec(query)){ | |
urlParams[decode(match[1])] = decode(match[2]); | |
} | |
return urlParams | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment