简体   繁体   English

区分按钮点击和JavaScript页面重新加载

[英]Differentiate between button click and javascript page reload

I'm trying to differentiate between page click and auto reload. 我试图区分页面点击和自动重新加载。 Auto reload is done through javascript below. 自动重新加载是通过下面的javascript完成的。

var pageReload = {
Initialize: function () {
      window.setTimeout("location.reload(true);", 60000);
}
pageReload.Initialize();

I'm trying to set a hidden variable in the above code for which I'm trying to check the changed value in Page_PreRender to understand the difference between page click and auto reload. 我试图在上面的代码中设置一个隐藏变量,为此我试图检查Page_PreRender更改的值以了解页面单击和自动重新加载之间的区别。

var hdnReloadType = document.getElementById('<%=hdnReloadType.ClientID%>');
hdnReloadType.value = "1";

The javascript is loaded after PreRender and I'm sure how to proceed. javascript是在PreRender之后加载的,我确定该如何进行。

Any thoughts? 有什么想法吗?

You could always use localStorage to leave a breadcrumb behind when your function reloads the page: 当您的函数重新加载页面时,您始终可以使用localStorage留下痕迹:

localStorage.setItem( 'page-reloaded', 'true' );

When the page loads, check for the breadcrumb: 页面加载后,检查面包屑:

var reloaded = localStorage.getItem('page-reloaded') || false;

And then cleanup afterwards: 然后清理:

localStorage.removeItem('page-reloaded');

Instead of just doing a reload() , you could add a "reloaded" parameter to the URL. 不仅可以执行reload() ,还可以在URL中添加一个“重载”参数。 If we use something like the replaceUrlParam() function from this answer , we can say: 如果我们从此答案中使用诸如replaceUrlParam()函数之类的东西,我们可以说:

var pageReload = {
  Initialize: function () {
    window.setTimeout(
      function() {
        var url = replaceUrlParam(window.location.href, 'reloaded', '1');
        window.location.href = url;
      },
      60000
    );
  }
}
pageReload.Initialize();

Now, when the page has been auto-reloaded (and only then), the query string will contain "reloaded=1". 现在,当页面被自动重新加载后(此后),查询字符串将包含“ reloaded = 1”。

Thank you for all the replies. 感谢您的所有答复。

Because I'm using asp.net, I was able to capture the previous page click url from Request object and from there I'm determining if this was a page auto reload or button click. 因为我使用的是asp.net,所以我能够从Request对象捕获上一页的单击URL,然后从那里确定这是页面自动重载还是按钮单击。 The query string reload option is great too but we didn't want to expose that to the user. 查询字符串重载选项也很棒,但是我们不想向用户公开。 This solution worked for my case. 此解决方案适用于我的情况。

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

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