2014-01-20 78 views

回答

0

從問號開始的部分URL稱爲查詢字符串。

這是一個純JavaScript函數來分析查詢字符串來獲得特定的值:

function querystring(key) 
{ 
    var filter; 
    var value; 

    key  = key.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]'); 
    filter = new RegExp('[\\?&]' + key + '=([^&#]*)'); 
    value = filter.exec(window.location.search); 

    if(value == null) 
    { 
     return ''; 
    } 
    else 
    { 
     return decodeURIComponent(value[1].replace(/\+/g, ' ')); 
    } 
} 

你只是通過在你感興趣的查詢字符串鍵值名(字符串),你得到的價值回(也作爲一個字符串。)你可以如何使用該功能的例子:

alert('Category = ' + querystring('catagory'));