window.onpopstate只影响一个页面范围范围、页面、window、onpopstate

由网友(橙家菇凉)分享简介:我有一个函数加载从数据库动态内容,它是在一定的链接点击名为:I have a function that loads dynamic content from a database, it is called when certain links are clicked on:的jQuery f...

我有一个函数加载从数据库动态内容,它是在一定的链接点击名为:

I have a function that loads dynamic content from a database, it is called when certain links are clicked on:

的jQuery

<script>
function loadContent(href){
$.getJSON("/route", {cid: href, format: 'json'}, function(results){  
$("#content_area").html("");
$("#content_area").append(results.content);
reinitFunc1();
reinitFunc2();
reinitFunc3();
// history.pushState('', 'New URL: '+href, href);
});
};
</script>

我想启用 pushState onpopstate

我可以通过取消上述 history.pushState 行启用网址,浏览器的历史变革 - 导航向前或向后跨页正常工作。

I can enable the URL and browser history change by uncommenting the above history.pushState line - navigating forwards and backwards across several pages works correctly.

但是,这并不加载内容。

But this does not load the content.

前阵子我的认为的我有完整的功能(即导航和内容的负载)在添加这些元素的:

A while back I think I had full functionality (ie navigation and content loading) with the addition of these elements:

window.onpopstate = function(event) {
console.log("pathname: "+location.pathname);
loadContent(location.pathname);
};

因此​​,我试图将它们添加到一个单独的模块:

I therefore tried adding them to a separate block:

<script>
$(document).ready(function() {
window.onpopstate = function(event) {
console.log("pathname: "+location.pathname);
loadContent(location.pathname);
};
});
</script>

但是,这导致页面导航范围将仅限于一个的时候,我不能浏览转发。

But this causes the page range when navigating to be limited to one, and I cannot navigate forwards.

我怎样才能做到既导航和内容加载使用上述code作为依据?

How can I achieve both navigation and content loading using the above code as a basis?

修改

和借鉴,正确的路径是在浏览器历史记录,这只是他们不能被导航(FF和铬)和相应的内容不会加载。在Firebug的任何错误。

And for reference, the correct paths are in the browser history, it's just they can't be navigated (FF and Chrome) and the corresponding content is not loading. No errors in Firebug.

推荐答案

我觉得这就是答案,我碰到这样的:

I think this is the answer, I came across this:

http://stackoverflow.com/a/10176315/1063287

还保证在onpopstate加载的页面()不要试图推动利用   pushState()自己。

"Also ensure that pages loaded in onpopstate() don't try and push use pushState() themselves".

所以,我一直这样的:

<script>
$(document).ready(function() {
window.onpopstate = function(event) {
console.log("pathname: "+location.pathname);
loadContent(location.pathname);
};
});
</script>

不过从功能中删除这样的:

But removed this from the function:

history.pushState('', 'New URL: '+href, href);

和添加它,而不是到了jQuery触发点击,例如:

And added it instead to the jQuery triggered on click eg:

$(document).on("click","#main_menu a", function (e) {
href = $(this).attr("href");
loadContent(href);
history.pushState('', 'New URL: '+href, href);
e.preventDefault();
});
阅读全文

相关推荐

最新文章