后退按钮 - 跳过锚点

发布于 2024-11-05 12:14:43 字数 133 浏览 0 评论 0原文

我有一个典型的 ajax 风格的动态内容设置:单击链接,更改地址栏中的锚点,然后加载新内容。当用户单击后退按钮时,它会按预期循环显示所有锚点。

有没有一种方法(我假设使用 JavaScript)使后退按钮转到上一页,并“忽略”所有锚点?

I have a typical ajax-style dynamic content setup: click a link, change anchor in address bar, and load new content. When user clicks the back button, it cycles through all the anchors - as expected.

Is there a way (using JavaScript I'm assuming) to make the back button go to the previous page, and 'ignore' all the anchors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偷得浮生 2024-11-12 12:14:43

计算用户通过和使用的阶段

history.back(X);

,或者

history.go(-1*X);

返回 X 个页面/锚点。

Count the stages the user pass and use

history.back(X);

or

history.go(-1*X);

in order to go back X pages/anchors.

箜明 2024-11-12 12:14:43

首先,为了确保我理解,您是否已将 AJAX 书签命名为与 DOM 元素中的 ID 相同的名称?如果是这样,无论如何要撤消这个吗?你可以欺骗它不让它下降,但这是一种笨拙的做法。相反,您应该拥有像 #somepage 这样的 ID 和像 #!/somepage 这样的 AJAX url,这样它们就不会混淆。此外,像 Google 这样的人不会知道普通的 #id 是哈希 URL,但是 #!/ 可以为 Google 提供线索。

现在,如果您想使用 #!/ 方法来执行此操作,您需要一个适用于旧版浏览器 (<=IE7) 的计时器,但对于“现代”浏览器 IE8、FF 和 Chrome,您可以使用onhashchange JS 属性如下:

window.onhashchange = function(){ console.log('hash has changed') };

然后,如果你想支持旧版浏览器,你需要做一些更高级的事情。您需要执行的操作:

var currentPage = '';
setTimeout(function(){
  if(currentPage !== window.location.hash){
    console.log('hash has changed');
    currentPage = window.location.hash
  }
},500);

在这两个示例中,您需要将 console.log() 替换为您自己的触发“更改页面”事件的函数。

要停止滚动,您可以添加:

window.onhashchange = function(){
  if (window.location.hash == "" || window.location.hash == "#"){ return false; }
  //rest of your code here!
};

return false 将阻止滚动。这也应该在计时器中起作用。

更多信息:
https://developer.mozilla.org/en/DOM/window.onhashchange
http://msdn.microsoft.com/en-us/库/cc288209(VS.85).aspx
http://ajaxpatterns.org/Unique_URLs

First, to make sure Im understanding, you have named your AJAX bookmarks the same as IDs in your DOM elements? If so, anyway to undo this? You can trick it not to go down, but this is kludgy way of doing it. Instead you should have a ID like #somepage and AJAX urls like #!/somepage this way they don't get confused. Also, people like Google will not know normal #ids are hash URLs, but doing #!/ gives Google a clue.

Now, if you want to do it using a #!/ method you need a timer for older browsers (<=IE7) But for "modern" browsers IE8, FF, and Chrome you can use the onhashchange JS property like:

window.onhashchange = function(){ console.log('hash has changed') };

Then, if you want to support older browsers you need to do something a little more advanced. You need to do:

var currentPage = '';
setTimeout(function(){
  if(currentPage !== window.location.hash){
    console.log('hash has changed');
    currentPage = window.location.hash
  }
},500);

In both examples you need to replace the console.log()s with a function of your own that triggers the "change page" event.

To stop the scrolling you could add:

window.onhashchange = function(){
  if (window.location.hash == "" || window.location.hash == "#"){ return false; }
  //rest of your code here!
};

The return false will prevent the scrolling. This should work in the timer as well.

More Information:
https://developer.mozilla.org/en/DOM/window.onhashchange
http://msdn.microsoft.com/en-us/library/cc288209(VS.85).aspx
http://ajaxpatterns.org/Unique_URLs

怂人 2024-11-12 12:14:43
location.replace(document.referrer)

这对我有用。根据您的目的,您可以在“onclick”中使用它或通过 JavaScript 添加一些附加条件,例如,如果引荐来源网址为空
看:
什么情况下 HTTP_REFERER 将为空

location.replace(document.referrer)

this works for me. depending on your purpose, you can use it in the "onclick" or add some additional conditions via JavaScript e.g. if the referrer is empty
See:
In what cases will HTTP_REFERER be empty

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文