1.vuejs取得URL中參數(shù)的值
1.http://localhost:3333/#/index?id=001
console.log(this.$route.query.id) //結(jié)果 001
2.// vue首頁直接匹配
getUrlKey (name) {
if (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href)) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [''])[1].replace(/\+/g, '%20')) || null
}
},
2. js獲取URL中的參數(shù)
js獲取URL中的一些參數(shù)的意思
location對(duì)象 含有當(dāng)前URL的信息. 屬性 href 整個(gè)URL字符串.
protocol 含有URL第一部分的字符串,如http:
host 包含有URL中主機(jī)名:端口號(hào)部分的字符串.如//www.cenpok.net/server/
hostname 包含URL中主機(jī)名的字符串.如http://www.cenpok.net ;
port 包含URL中可能存在的端口號(hào)字符串.
pathname URL中"/"以后的部分.如~list/index.htm
hash "#"號(hào)(CGI參數(shù))之后的字符串.
search "?"號(hào)(CGI參數(shù))之后的字符串.
1.第一種:只適用于/User/vip_card_manager?useless=219
function UrlSearch() {
var name,value;
var str=location.href; //取得整個(gè)地址欄
var num=str.indexOf("?")
str=str.substr(num+1); //取得所有參數(shù) stringvar.substr(start [, length ]
var arr=str.split("&"); //各個(gè)參數(shù)放到數(shù)組里
console.log(arr)
for(var i=0;i < arr.length;i++){
num=arr[i].indexOf("=");
if(num>0){
name=arr[i].substring(0,num);
value=arr[i].substr(num+1);
this[name]=value;
}
}
}
2.第二種:
適應(yīng)以下兩種模式,來獲取url參數(shù)值:
/User/vip_card_manager/useless/219/id/18
/User/vip_card_manager?useless=219&id=18
console.log(getQueryString("useless"));
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
var r = window.location.search.substr(1).match(reg);
var q = window.location.pathname.substr(1).match(reg_rewrite);
if(r != null){
return unescape(r[2]);
}else if(q != null){
return unescape(q[2]);
}else{
return null;
}
}
3.第三種:
/**
* 獲取指定的URL參數(shù)值
* URL:http://www.quwan.com/index?name=tyler
* 參數(shù):paramName URL參數(shù)
* 調(diào)用方法:getParam("name")
* 返回值:tyler
*/
function getParam(paramName) {
paramValue = "", isFound = !1;
if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"), i = 0;
while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0), i++
}
return paramValue == "" && (paramValue = null), paramValue
}