简体   繁体   English

复选框/后退按钮问题取决于浏览器

[英]checkbox/backbutton issue depending on the browser

I've implemented a website in .jsp / JSTL / EL that works fine. 我在.jsp / JSTL / EL中实现了一个工作正常的网站。 However, depending on the browser (!), the behavior of the back button is varying a bit. 但是,根据浏览器(!),后退按钮的行为会有所不同。

I did take care to not have double post (so that the browser does not warn the user that he's going to post again) but then apparently this leads to another issue: if I use the back button from Chrome, then the value of the checkboxes aren't correct. 我确实注意没有双重帖子(这样浏览器不会警告用户他会再次发布)但是显然这会引发另一个问题:如果我使用Chrome的后退按钮,那么复选框的值不正确。

Here's the scenario as I understand it: 这是我理解的场景:

  1. user is on page /page1 then checks a checkbox 用户在页面/ page1上然后选中一个复选框
  2. a POST is made, there's a HTTP redirect 进行了POST,有一个HTTP重定向
  3. a GET is made, page /page2 displays fine, with the checkbox checked as it should be 进行GET,页面/页面2显示正常,并选中复选框
  4. user hits the back button 用户点击后退按钮
  5. user goes back to /page1 as it was before the checkbox was checked (which is exactly the expected behavior for the back button) but the checkbox is still checked. 用户返回到/ page1,就像检查复选框之前一样 (这正是后退按钮的预期行为), 仍然选中了复选框。

From looking at the server's log and my application's log as far as I can see there's absolutely nothing transmitted to the webserver. 从我看到服务器的日志和我的应用程序的日志,我绝对没有传输到网络服务器。 So it's purely on the client side that things are happening. 因此,纯粹在客户端,事情正在发生。

What is going on and how can I fix this? 发生了什么,我该如何解决这个问题? (without using a framework, it's not what this question is about) (没有使用框架,这不是这个问题的意思)

EDIT I should probably give a bit more info... The webapp is already a bit "fancy" in that each page served --in a tiny URL-like fashion-- corresponds to a state. 编辑我应该提供更多信息...... webapp已经有点“花哨”了,每个页面服务 - 以一种像URL一样的小方式 - 对应一个状态。 Everything is "public": anyone can see anyone's pages (but it could be private, with private URLs). 一切都是“公开的”:任何人都可以看到任何人的页面(但它可以是私人的,有私人网址)。 Anyway: if a user shares any of its page, when another user shall open a page it shall display correctly and the checkboxes are correctly initialized. 无论如何:如果用户共享其任何页面,当另一个用户打开一个页面时,它应该正确显示并且复选框正确初始化。

All this to say that the problem is not on the server side, where the "state" somehow wouldn't be correct: if user A share pages .../1d2eof then user B opening the page .../1d2eof shall see the correct page. 所有这一切都表明问题不在服务器端,“状态”某种方式不正确:如果用户A共享页面... / 1d2eof那么用户B打开页面... / 1d2eof将看到正确的页面。

The only times where this gets messed up is when the user uses the back button. 只有当用户使用后退按钮时才会弄乱它。 Remember that, at any point, I've got the full page's state accessible on the server side. 请记住,在任何时候,我都可以在服务器端访问整页的状态。 However my issue is that apparently when using the back button nothing contacts my server, so I don't get the chance to "fix the page" for the user who used the back button. 但是我的问题是,显然当使用后退按钮时, 没有任何东西可以与我的服务器联系,所以我没有机会为使用后退按钮的用户“修复页面”。

Maybe a double redirect would help!? 也许双重定向会有所帮助!?

This is how i solved mine using a combo of hiddenfield and the querystring. 这就是我使用hiddenfield和querystring的组合来解决我的问题。

<script type="text/javascript">
    jQuery(document).ready(function () {

    if (!(window.location.toString().indexOf('&FIP') >= 0) && (document.getElementById("<%= hdnField.ClientID %>").value.toString() == "BackButtonClickIsSet")) {
        document.location.reload(true);
    }

    if (!(window.location.toString().indexOf('&FIP') >= 0)) {
        document.getElementById("<%= hdnField.ClientID %>").value = "BackButtonClickIsSet";
    }

    if (window.location.toString().indexOf('&FIP') >= 0) {
        document.getElementById("<%= hdnField.ClientID %>").value = "BackButtonClickIsNotSet";
    }

});
</script>


<asp:HiddenField ID="hdnField" runat="server" Value="BackButtonHiddenField" />

This is a browser feature: pressing the "back" button will restore the final state of the form. 这是一个浏览器功能:按“后退”按钮将恢复表单的最终状态。 If you want to reset the form contents to what was specified in the HTML (ie, to a state before user interaction), you can use document.formName.reset() , where formName is the name of the form. 如果要将表单内容重置为HTML中指定的内容(即,用户交互之前的状态),可以使用document.formName.reset() ,其中formName是表单的名称。 (Unfortunately, it's a bit hard to detect that the user has hit the "back" button and returned to the current page, so it's tricky to figure out when to run the above bit of code.) (不幸的是,有点难以检测到用户已经点击“返回”按钮并返回到当前页面,因此弄清楚何时运行上面的代码是很棘手的。)

You might want to take a look at http://www.bajb.net/2010/02/browser-back-button-detection/ 你可能想看看http://www.bajb.net/2010/02/browser-back-button-detection/

It offers the ability to get control over what happens on the client side (and possibly server side with ajax) when the back button is clicked. 它提供了在单击后退按钮时控制客户端(以及可能是带有ajax的服务器端)上发生的事情的能力。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM