JQuery - get page?
I nearly have this going but don't have sufficient brain power to work out the final step.
This is what happens at the moment.
1) Page loads 2) When a link is clicked, a hash is added to the url with the page id. This is saved to 开发者_StackOverflowthe browser history.
I have the Ajax call set up, if I attach it to the link function it works fine. The problem is getting the id from the url. Here is what I have.
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 Error: url.split is not a function.
I sense that if I can just get rid of the JavaScript error I should be golden.
Try this:
function urlToId() {
return window.location.hash.substr(1);
}
Your url parameter refers to the window.location object which does not have a split function (it is not a string).
Try window.location.hash
instead, this will return just from the # onwards as a string. Rather than the window.location object.
var id = hashToId(window.location.hash);
if (id != undefined) {
go(id);
}
function hashToId(hash) {
return hash.slice(1); // remove the leading #
}
精彩评论