2013-07-31 115 views
0
console.log(_GET("appid")); 

in fucn _GET我需要檢查param是否存在,如果存在則返回它。JS從網址hash中獲取參數

function _GET(paramName) { 
    var hash = window.location.hash; // #appid=1&device&people= 

    //this needs to be not static 
    if (/appid=+\w/.test(window.location.hash)) { 
     //and somehow parse it and return; 
    } 
    return false; 
} 

我希望看到在控制檯1,如果我CONSOLE.LOG(_GET(「設備」))或人則空

回答

0

您需要使用String.match並通過在RegExp對象代替:

function _GET(paramName) { 
    var pattern = paramName + "=+\w"; 
    return (window.location.hash.match(new RegExp(pattern, 'gi'))) != null; 
} 
0
function _GET(paramName) { 
    var hash = window.location.hash.match(new RegExp("appid=+\w", 'gi')); // #appid=1&device&people= 

    //this needs to be not static 
    if (hash) { 
     //and somehow parse it and return; 
    } 
    return false; 
} 
+0

if(!anchro)也可以,但是我怎樣才能從錨點解析值 – user840250