0

我有这个网址index.html#secondPage?name=the%20second%20page

我想使用 javascript 和 jquery 获取名称(“第二页”)的值

谢谢

4

2 回答 2

0

您可以使用此问题答案中的代码。唯一的区别是您想要解析location.hash而不是location.search所以只需在您选择使用的任何答案中更改它。

您还需要用于substr删除前导#,例如:

var hash = window.location.hash.substr(1);

这是我对链接到的问题的回答中的代码,并进行了修改:

function get_query(){
    var url = location.hash.substr(1);
    var qs = url.substring(url.indexOf('?') + 1).split('&');
    for(var i = 0, result = {}; i < qs.length; i++){
        qs[i] = qs[i].split('=');
        result[qs[i][0]] = decodeURIComponent(qs[i][1]);
    }
    return result;
}

你可以像这样使用它:

var secondPage = get_query()['name']; // 'the second page'
于 2013-04-16T15:36:01.317 回答
0

试试下面的方法,它会帮助你

小提琴:http: //jsfiddle.net/RYh7U/144/

Javascript:

function GetURLValue (sKey) {
  return unescape("index.html#secondPage?name=the%20second%20page".replace(new RegExp("^(?:.*[&\\?]" + escape(sKey).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}

alert(GetURLValue("name"));
于 2013-04-16T15:40:17.853 回答