2015-09-29 46 views
0

我有以下EX- URL- "/MyProject/Information/EmpDetails.aspx?userId=79874&countryId=875567"拆分最後一個字值

在JavaScript中的URL字符串現在,我需要做以下兩件事情

  1. 檢查該國是否存在上述網址或不併有將在上述網址

  2. 只有一個countryId獲取countryId值意味着875567.

謝謝你們對這種良好的反應很快。我得到了解決大部分的答案是正確的。

一個更多的問題的人我有超鏈接,所以我在onmousedown事件時產生一些活動。但問題是它甚至當我只點擊右鍵時觸發..但我想點擊超鏈接雙擊時觸發的事件或者右鍵單擊,然後單擊

回答

0

您需要使用的組合indexOf()substring()

var ind = url.indexOf("countryId"); 
if (ind != -1){ 
    // value is index of countryid plus length (10) 
    var countryId = url.substring(ind+10); 
}else{ 
    //no countryid 
} 
1

使用

window.location.href

而且

與斯普利特獲取URL '?'第一,「&」未來和「=」,這樣你可以得到countryId

OR

直接拆分爲「=」,並從數組最後一個值後分裂

0

,我們得到如何像這樣的:

var TheString = "/MyProject/Information/EmpDetails.aspx?userId=79874&countryId=875567"; 

var TheCountry = parseInt(TheString.split('=').pop(), 10); 

然後你只需要如果TheCountry測試是一些與if (TheCountry) { ...}

這當然假定URL查詢字符串在結尾處始終具有國家/地區ID。

0
var url ='/MyProject/Information/EmpDetails.aspx?userId=79874& countryId=875567'; 
alert((url.match(/countryId/g) || []).length); 
alert(url.substring(url.lastIndexOf('=')+1)); 

你可以在第一次警報的任何字符串的發生的計數和子得到countryid值。

0

這將您的網址查詢轉換成一個對象

var data = url.split('?')[url.split('?').length - 1].split('&').reduce(function(prev, curr){ 
    var fieldName = curr.split('=')[0]; 
    var value = curr.split('=').length > 1 ? curr.split('=')[1] : ''; 
    prev[fieldName] = value; 
    return prev 
}, {}); 

那麼你可以檢查data.country值來獲取值

0

您也可以分割字符串,看看countryId存在,如下所示。

var myString = "/MyProject/Information/EmpDetails.aspx?userId=79874&countryId=875567"; 
myString = myString.split("countryId="); //["/MyProject/Information/EmpDetails.aspx?userId=79874&", "875567"] 
if (myString.length === 2) { 
    alert (myString.pop()); 
} 
相關問題