Last active
April 22, 2019 13:43
-
-
Save meigesir/8108077 to your computer and use it in GitHub Desktop.
Javascript:获取url键值对,修改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
| /*获取url键值对*/ | |
| function getParamsFromUrl() { | |
| var url = decodeURI(location.search); //获取url中"?"符后的字串 | |
| var params = new Object(); | |
| if (url.indexOf("?") != -1) { | |
| var str = url.substr(1); | |
| const strs = str.split("&"); | |
| for(var i = 0; i < strs.length; i ++) { | |
| params[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); | |
| } | |
| } | |
| return params; | |
| } | |
| /* | |
| * url 目标url | |
| * arg 需要替换的参数名称 | |
| * arg_val 替换后的参数的值 | |
| * return url 参数替换后的url | |
| */ | |
| function changeURLArg(url,arg,arg_val){ | |
| var pattern=arg+'=([^&]*)'; | |
| var replaceText=arg+'='+arg_val; | |
| if(url.match(pattern)){ | |
| var tmp='/('+ arg+'=)([^&]*)/gi'; | |
| tmp=url.replace(eval(tmp),replaceText); | |
| return tmp; | |
| }else{ | |
| if(url.match('[\?]')){ | |
| return url+'&'+replaceText; | |
| }else{ | |
| return url+'?'+replaceText; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment