浏览器的“后退"按钮导致隐藏字段出现问题 [英] Browsers back button is causing issues with hidden field

查看:105
本文介绍了浏览器的“后退"按钮导致隐藏字段出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个有趣的问题,但不确定如何解决.我在主页上保留了一个隐藏值,该值跟踪页面上加载的事件.页面加载后,事件位置会更新.当离开页面导航,然后单击浏览器的后退按钮时,将看到先前的事件编号.如果我单击链接并直接加载主页,则事件位置将重置(如预期)

I've run into an interesting issue and I'm not sure how to solve it. I maintain a hidden value on my homepage that tracks the events loaded on the page. The event position gets updated after the page loads. When navigating away from the page and then clicking the browsers back button the previous event number is seen. If I click a link and load the home page directly the event position gets reset (as expected)

我的HTML:

    <input type="hidden" id="event_num" value="0">

我的JavaScript调用以获取/设置event_num值:

My javascript call to get/set the event_num value:

 function getRecentEvents() {

    var event_pos = $('#event_num').val();
    //console.log("POS: " + event_pos);

    $.getJSON("functions/get_events.php", { f: 'get_events', event_pos: event_pos, limit: limit}, function(data) {

        if (data.events.length > 0) {

            // set the new value        
            $('#event_num').val(data.events_pos);
        }
});

无论用户单击链接还是单击浏览器上的后退按钮,每次加载页面时,我都希望隐藏字段为 0 .

I would expect the hidden field to be 0 every time the page is loaded regardless if the user clicked a link or hit the back button on the browser.

有什么主意我可以解决这个问题吗?或者这是一个已知问题?

Any idea how I might fix this or is this a known issue?

推荐答案

如果您单击后退"按钮,则不会再次加载该页面,将显示缓存的页面内容.也就是说,页面的状态是您上次访问它的时间.您可以在$(document).ready或文档卸载事件上重置event_num计数器,然后将获得预期的行为.

The page is not loaded again if you click the "back" button, the cached page content is shown. That is, the page as the state of it was the last time you visited it. You could reset the event_num counter on $(document).ready or on a document unload event, then you will get the expected behaviour.

这篇关于浏览器的“后退"按钮导致隐藏字段出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆