2010-06-21 62 views
0

我几乎要这样做,但没有足够的智力来制定最后一步。JQuery - 获取页面?

这就是目前发生的情况。

1)页面加载 2)当单击一个链接时,一个散列被添加到带有页面标识的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; 
} 

警报(URL)= http://localhost/site/index.php?p=1#1 - JavaScript错误:url.split不是一个函数。

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

回答

1

试试这个:

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

你的url参数指的是没有分割函数的window.location对象(它不是一个字符串)。

尝试window.location.hash取而代之的是,它将以字符串的形式从#开始。而不是window.location对象。

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

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