JQuery - 获取页面?

时间:2010-06-21 09:46:52

标签: jquery

我几乎已经开始了,但没有足够的脑力来完成最后一步。

这就是目前发生的事情。

1)页面加载 2)单击链接时,会使用页面ID将哈希添加到URL。这将保存到浏览器历史记录中。

我有Ajax调用设置,如果我将它附加到链接功能,它工作正常。问题是从网址获取id。这就是我所拥有的。

var id = urlToId(window.location);
if (id != undefined) {
    go(id);
}

function urlToId(url) { alert(url);
    var segments = url.split('#'); alert(segments);
    var id = segments[1];
    return id;
}

alert(url)= http://localhost/site/index.php?p=1#1 - Javascript错误:url.split不是函数。

我觉得如果我能摆脱JavaScript错误,我应该是金色的。

2 个答案:

答案 0 :(得分:1)

试试这个:

function urlToId() {
    return window.location.hash.substr(1);
}

答案 1 :(得分:0)

你的url参数引用了window.location对象,它没有split函数(它不是一个字符串)。

请尝试使用window.location.hash,这将仅从#onwards作为字符串返回。而不是window.location对象。

var id = hashToId(window.location.hash); 
if (id != undefined) { 
    go(id); 
} 

function hashToId(hash) {
    return hash.slice(1); // remove the leading #
}