2016-02-28 33 views
1

我讀過一些問題,但我仍然無法弄清楚如何做到這一點
我有一個網址example.com/event/14aD9Uxp?p=10的JavaScript獲取URL段和參數

在這裏,我想獲得14aD9Uxp和P的值
我一直在使用split('/'+'?p=')嘗試,但它不工作
我想用正則表達式,但我不真正瞭解如何使用它

+0

''通過/'split',拿到最後一個元素。再次用'?'分隔,第一個是'14a ....',再次用'='分隔,第二個來自最後一個分割數組。 – Tushar

+0

正則表達式[\\([^ \ /?] *?)\?p =(\ d +)$'](https://regex101.com/r/vD5cF2/1) – Tushar

+0

您可能想要看看[**'location' **](https://developer.mozilla.org/en-US/docs/Web/API/Location)對象。 – Leo

回答

3
var URL='example.com/event/14aD9Uxp?p=10'; 

var arr=URL.split('/');//arr[0]='example.com' 
         //arr[1]='event' 
         //arr[2]='14aD9Uxp?p=10' 

var parameter=arr[arr.length-1].split('?');//parameter[0]='14aD9Uxp' 
              //parameter[1]='p=10' 

var p_value=parameter[1].split('=')[1];//p_value='10'; 
+0

感謝它的工作。對於最後一部分,我使用'split('?p =');'所以我直接得到p值 – Spadaboyz

+0

你知道它然後.....好 – Flake

0

簡單無正則表達式的方式

var s = "example.com/event/14aD9Uxp?p=10"; 
var splitByForwardSlash = s.split('/'); 

// To get 14aD9Uxp 
splitByForwardSlash[splitByForwardSlash.length-1] 

// To get p=10 
splitByForwardSlash[splitByForwardSlash.length-1].split('?')[1] 

我想你知道如何去從這裏:-)

1

我創建了一個廣義函數(在某些方面的限制),將返回給定參數的GET值。但是,只有在不重寫URL或修改URL GET SYNTAX時,此功能才能正常工作。

對不起,這可能會有點大檢查。但是這些天我一直渴望寫出一些好的代碼。

//Suppose this is your URL "example.com/event/14aD9Uxp?p=10"; 
      function GET(variable) { 
       var str = window.location.href; 


       str = str.split("/"); 
       // str = [example.com, event, 14aD9Uxp?p=10] 


       //Get last item from array because this is usually where the GET parameter is located, then split with "?" 
       str = str[str.length - 1].split("?"); 
       // str[str.length - 1] = "14aD9Uxp?p=10" 
       // str[str.length - 1].split("?") = [14aD9Uxp, p=10] 

       // If there is more than 1 GET parameter, they usually connected with Ampersand symbol (&). Assuming there is more, we need to split this into another array 

       str = str[1].split("&"); 
       // Suppose this is your URL: example.com/event/14aD9Uxp?p=10&q=112&r=119 
       // str = [p=10, q=112, r=119] 
       // If there is only 1 GET parameter, this split() function will not "split" anything 

       //Remember, there might only be 1 GET Parameter, so lets check length of the array to be sure. 
       if (str.length > 1) { 

        // This is the case where there is more than 1 parameter, so we loop over the array and filter out the variable requested 
        for (var i = 0; i < str.length; i++) { 

         // For each "p=10" etc. split the equal sign 
         var param_full_str = str[i].split("="); 
         // param_full_str = [p, 10] 

         //Check if the first item in the array (your GET parameter) is equal to the parameter requested 
         if (param_full_str[0] == variable) { 
          // If it is equal, return the second item in the array, your GET parameter VALUE 
          return param_full_str[1]; 
         } 
        } 
       } else { 
        // This is the case where there is ONLY 1 GET parameter. First convert it to a String Type because Javascript decided that str was no longer a String 
        // Now split it with the equal sign. 
        str = str.toString().split("="); 
        return str[1]; 
       } 
      } 
      document.write(GET("p")); 
0
function $_GET(param) { 
    var vars = {}; 
    window.location.href.replace( 
     /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp 
     function(m, key, value) { // callback 
      vars[key] = value !== undefined ? value : ''; 
     } 
    ); 

    if (param) { 
     return vars[param] ? vars[param] : null;  
    } 
    return vars; 
} 

我從這裏收集此: http://www.creativejuiz.fr/blog/javascript/recuperer-parametres-get-url-javascript 它的偉大工程。

要使用它,只需抓住你的參數,如:

var id = $_GET('id');