检测浏览器的刷新 [英] Detect browser refresh

查看:139
本文介绍了检测浏览器的刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能找到,如果用户pressed F5刷新我的网页(喜欢的东西怎么这么实现的。如果您刷新页面,问题计数器不增加)。我测试的教程十几提到许多code片段,但没有工作正常。

要更清楚,假设我有一个空的Web表单,并想检测用户是否在客户端pssed F5 $ P $(导致刷新不提交)或不。

我可以使用会话变量,但如果用户浏览到我的网站的另一个页面,然后再回来,我想将它作为新的访问,没有刷新。所以这不是一个会话范围的变量。

感谢。

更新:我能找到的唯一解决方法是继承基页我的网页,覆盖负载方法象下面这样:

 公共类PageBase:System.Web.UI.Page
{
    保护覆盖无效的OnLoad(EventArgs的发送)
    {
        base.OnLoad(E);
        this.Session [LastViewedPage] = Request.RawUrl;
    }
}

和在每一个页面,如果我有兴趣知道,如果这是一个刷新:

 如果(this.Session [LastViewedPage]。的ToString()== Request.RawUrl)
{
    //这是一个刷新!
}


解决方案

唯一确定的解决方案是让的重定向到同一个页面 ,这里是一个类似的问题:的后重定向,获取与ASP.NET

不过,也有一些其他的技巧,通过添加页面上的一些票,看看这是相同的或有变化,请参阅完整的例子,code:

http://www.$c$cproject.com/Articles/68371/Detecting-Refresh-or-Postback-in-ASP-NET

和多了一个:

<一个href=\"http://dotnetslackers.com/community/blogs/simoneb/archive/2007/01/06/Using-an-HttpModule-to-detect-page-refresh.aspx\" rel=\"nofollow\">http://dotnetslackers.com/community/blogs/simoneb/archive/2007/01/06/Using-an-HttpModule-to-detect-page-refresh.aspx

How can I find out if the user pressed F5 to refresh my page (Something like how SO implemented. If you refresh your page, the question counter is not increased). I have tested many code snippets mentioned in a dozen of tutorials, but none worked correctly.

To be more clear, suppose that i have an empty web form and would like to detect whether the user has pressed F5 in client-side (causing a refresh not submit) or not.

I can use session variables, but if the user navigates to another page of my site, and then comes back , I'd like to consider it as a new visit, not a refresh. so this is not a session-scope variable.

Thanks.

Update: The only workaround I could find was to inherit my pages from a base page, override the load method like below:

    public class PageBase : System.Web.UI.Page
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.Session["LastViewedPage"] = Request.RawUrl;
    }
}  

and in every page if I was interested to know if this is a refresh:

if (this.Session["LastViewedPage"].ToString() == Request.RawUrl)
{
    // This is a refresh!
}

解决方案

The only sure solution is to make a redirect to the same page , and here is a similar question: Post-Redirect-Get with ASP.NET

But there are also some other tricks, by adding some ticket on the page and see if this is the same or have change, see the full example and code at:

http://www.codeproject.com/Articles/68371/Detecting-Refresh-or-Postback-in-ASP-NET

and one more:

http://dotnetslackers.com/community/blogs/simoneb/archive/2007/01/06/Using-an-HttpModule-to-detect-page-refresh.aspx

这篇关于检测浏览器的刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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