Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight 4: Detect browser F5 / refresh and X / Close

I want to determine how to filter F5, refresh button, X and close in browser via silverlight 4.0 or even in server side.

thank you

EDITED:

I added bounty to my question just today, July 28 2011. My previous solution / answer is no longer working in IE 9.

window.onunload = function (e) {
    // Firefox || IE
    e = e || window.event;
    var y = e.pageY || e.clientY;

    if (y < 0) {
        alert("close");
    }
    else {
        alert("refresh");
    }
}

When the user hit F5, refresh, X and close button, message box should NOT appear. Just in case the solution is onbeforeunload.

Thanks for you help!

like image 526
xscape Avatar asked Aug 11 '10 08:08

xscape


2 Answers

It is not possible client-side to determine whether an application startup is the result of a refresh operation performed by the user.

However you can determine at serverside that a page is being refreshed. You can add the following property to the code-behind of the ASPX page hosting the Silverlight application.

public bool IsRefresh
{
   get { Request.Headers["pragma"] ?? "").Contains("no-cache"); }
}

Now you use this property to conditionally include a value in the silverlight plugin initParams.

<object ...>
   <param name="initParams" value="IsRefresh=<%=IsRefresh.ToString()%>" />
</object>

Then in silverlight code you can determine if the application was last loaded as a result of a refresh with:-

if (Application.Current.Host.InitParams["IsRefresh"]  == "True")
like image 125
AnthonyWJones Avatar answered Nov 19 '22 09:11

AnthonyWJones


since it is not possible in client side, i did it in server side.

I solve my problem using this code:

window.onunload = function (e) {
        // Firefox || IE
        e = e || window.event;
        var y = e.pageY || e.clientY;

        if (y < 0) {
            alert("close");
        }
        else {
            alert("refresh");
        }
    }
like image 21
xscape Avatar answered Nov 19 '22 08:11

xscape