简体   繁体   English

History.go(-1)不适用于具有指向同一页面部分的链接的页面

[英]History.go(-1) not working for the page that has links to the sections of same page

I am facing a problem. 我正面临一个问题。 I have a web page where I use back button anchor link and the code used is ONCLICK="history.go(-1)" . 我有一个网页,我使用后退按钮锚链接,使用的代码是ONCLICK="history.go(-1)" The same page has many anchor links and the related sections. 同一页面有许多锚链接和相关部分。

When I go to a page containing back button and click back button immediately, then back button works fine. 当我转到包含后退按钮并立即单击后退按钮的页面时, 后退按钮工作正常。

If I go to a page containing back button, then hit anchor link that points to a section in the same page and then hit the back button, then it never goes to previous web page, instead goes to the section url. 如果我转到包含后退按钮的页面,然后点击指向同一页面中某个部分的锚点链接然后点击后退按钮,则它永远不会转到上一个网页,而是转到部分网址。

Through the new History API, you can manipulate the browser history . 通过新的History API,您可以操纵浏览器历史记录

History.js helps you achieve this in a cross-browser way. History.js可帮助您以跨浏览器的方式实现此目的。

I have quickly created a small fiddle that illustrates it working and uses jQuery for event handling (this is not production code, just a demo, works in my Chrome). 我已经快速创建了一个小小提示,说明它正在工作并使用jQuery进行事件处理(这不是生产代码,只是一个演示,可以在我的Chrome中运行)。

$('.anchor').click(function () {
    history.replaceState({}, '', this.href);
});

If so you could use javascript to solve this by simply using this line. 如果是这样,您可以使用javascript通过简单地使用此行来解决此问题。

Make use of the history.go() JavaScript function as below: 使用history.go() JavaScript函数如下:

<a href="javascript:window.history.go(-1);">Go back</a>

Maybe you can try a counter that counts how many times a "section" link is clicked 也许你可以尝试一个计数器来计算点击“部分”链接的次数

Maybe something like (with jQuery but maybe some kind person will go and change this code to nice plain JavaScript): 也许类似的东西(使用jQuery,但也许某些人会去,并将此代码更改为漂亮的纯JavaScript):

var count = 0;
var historyValue = -1;
$(".sectionlinks").click(function () {
    ++count;
    window.historyValue = -(count + 1);
})​

And all your section links have a sectionlinks class. 并且您的所有部分链接都有sectionlinks类。

Then you can do on your back link: 然后你可以在你的反向链接上做:

<a onclick="history.go(historyValue)">My Back Link</a>

See this fiddle for a demo: http://jsfiddle.net/RRcyF/2/ 请看这个小提琴演示: http//jsfiddle.net/RRcyF/2/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 <input type=button value=“Previous Page” onClick=“history.go(-1)”>警告:网页已过期IE - <input type=button value=“Previous Page” onClick=“history.go(-1)”> Warning: Page has Expired IE Javascript; 浏览器历史记录; 是否有跨浏览器的方式来读取上一页url(history.go(-1))和倒数第二页(history.go(-2))? - Javascript; Browser history; Is there cross-browser way to read previous page url( history.go(-1) ), and second last page ( history.go(-2) )? History.go(-1)不在IE中工作 - History.go(-1) not working in IE Javascript历史记录。如果不是当前站点,请默认转到页面 - Javascript History.Go default to page if not current site 导航到其他页面后将jquery history.go()转到特定页面 - jquery history.go() to a specific page after navigating to different pages javascript:history.go无法刷新手机上的页面? - javascript:history.go does not refresh page on mobile phone? PHP是否有可能在每个Javascript history.go(-1)上生成一个新页面? - Is it possible for PHP to generate a fresh page on every Javascript history.go(-1)? 使用Javascript history.go()到真正的“上一页” - Use Javascript history.go() to the real “previous” page javascript:history.go(-1)在Internet Explorer 7中不起作用 - javascript: history.go(-1) not working in internet explorer 7 history.go(-1)在Internet Explorer中不起作用 - history.go (-1) not working in internet explorer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM