11

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!

xscape
  • 3,318
  • 9
  • 45
  • 86
  • Filter how - intercept it and prevent the user doing it? I doubt that's possible. – Rup Aug 11 '10 at 08:26
  • I just want to know how, not to prevent but to reload the web service. – xscape Aug 11 '10 at 08:28
  • What you mean by reload web service? If web service is in some non default state, add some state variable, that application will set at start up and so every restart will reset this variable and you'll get event out of it? – Zee Jul 29 '11 at 14:18
  • Thanks Zeela, however, I need to identify if the user close or just refresh the browser. Do you have any other suggestions? – xscape Aug 01 '11 at 04:44
  • To all my stackoverflow fellas, any update with this one? Thanks – xscape Aug 04 '11 at 08:23

3 Answers3

2

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")
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • * Request.Headers["Accept-Encoding"] * Request.Headers["Accept-Charset"] * Request.Headers["Accept-Language"] * Request.Headers["Accept"] All of these 4, when I hit F5, IsRefresh is still False, did I miss anything? – xscape Aug 12 '10 at 07:34
  • Even if I change it to this, Request.Headers["pragma"].Contains("no-cache"); OR (Request.Headers["pragma"] ?? "").Contains("no-cache"); This is the error: Object reference not set to an instance of an object – xscape Aug 12 '10 at 07:41
2

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");
        }
    }
xscape
  • 3,318
  • 9
  • 45
  • 86
0

There is no property to check if your application is loaded by pressing the F5-button but you could handle the application startup event and set a variable with a datetime. The moment your page gets loaded you can check if the timespan is just a couple of seconds ago. So now you know that the application is loaded the first time or the F5-button is pressed when that time it is only a couple of seconds ago. I don't know if this is sufficient for you but you can give it a try:

App.xaml.cs

public class App : Application
{
 private DateTime appStartupTime {get; set};
 public App()
 {
     Startup += new EventHandler(Application_Startup);
 } 

 void Application_Startup(object sender, StartupEventArgs e)
 {
   //initialize the startupTime
   appStartupTime = DateTime.Now; 
 }
 public bool IsApplicationReLoaded
 {
   get
   {
     //return true if your app is started less 10 seconds ago
     return DateTime.Now.AddSeconds(-10) < appStartupTime;
   }
  }
}

Now you can start using the code below from everywhere

(Application.Current as App).IsApplicationReloaded
Wouter Janssens
  • 1,593
  • 10
  • 17