Last active
July 2, 2018 02:53
-
-
Save ayuLiao/9a729bf2db46d797356609ca72400fbc to your computer and use it in GitHub Desktop.
JavaScript获取url中的值
This file contains hidden or 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 getQueryString = function (name, url) { | |
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); | |
var search; | |
if (url === undefined) { | |
url = decodeURI(location.href); | |
} | |
var index = url.indexOf("?"); | |
search = url.substring(index); | |
var r = search.substr(1).match(reg); | |
if (r != null)return unescape(r[2]); | |
return null; | |
}; | |
// www.ayuliao.com?name=ayuliao&age=28 | |
var name = getQueryString("name"); // 'ayuliao' 都是str类型 | |
var age = getQueryString("age"); // '28' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment