Problem onunload event in IE and Safari

1

First of all, hello everyone and thanks for your time.

I'm making an application with Jquery Mobile and I have a big problem. When a page is refreshed, all the dynamic content that has been generated from the page is obviously lost, leaving only the HTML template where the content has been generated dynamically.

What I am trying to do is that when the user tries to refresh the page, he asks if he really wants to do it, since all the information on the page will be lost, this is already implemented and he asks it when the user wants to refresh. the page.

But the problem comes when I try to redirect the page to the home page. It works correctly for Chrome, Firefox and Opera, but not for Internet Explorer and Safari ...

The code is simple:

window.onunload = function(e){

$.mobile.changePage("#login");
window.location.reload();

}

What I do in case of confirmation by the user that you want to refresh the page is to redirect it to the login and make a refresh, to "force" to load the entire document, because if I do not leave me operate all the handlers.

Well the fact is that this works well in these browsers but not in IE and Safari, it seems that IE ignores the redirection that I ask and directly reloads the page on which the user is , with the inconvenience of what was previously mentioned, the page is reloaded without any dynamic content.

Does anyone know why this may happen ?, Internet Explorer does not let you do any kind of redirect or something ?. I tried to do it through window.location, window.location.href and window.location.replace and nothing keeps reloading the page the user is currently on.

Any solution?

Thanks in advance.

    
asked by David 05.07.2016 в 11:47
source

2 answers

1

Using jQuery you can in the same definition of changepage define the reloadpage which is especially what you are interested in:

$.mobile.changePage(
  "#login",
  {
    allowSamePageTransition : true,
    transition              : 'none',
    showLoadMsg             : false,
    reloadPage              : true
   }
);

If you look at the documentation you will see that there are several different options in changepage

The problem you have with IE and Safari surely comes in that they do not do exactly the same after changepage .

Your function onunload would be like this:

window.onunload = function(e){
    $.mobile.changePage(
      "#login",
      {
        allowSamePageTransition : true,
        transition              : 'none',
        showLoadMsg             : false,
        reloadPage              : true
       }
    );
}

Without adding window.location.reload(); you should work.

With jQuery 1.4 or higher the changePage is deprecated and used pagecontainer :

$.mobile.pageContainer.pagecontainer("change", "#login", , {
    transition: 'flow',
    reload    : true
  });
    
answered by 05.07.2016 в 15:10
0

In the end I solved it without doing anything in the onunload event, since each browser "manages" this event in some way, some if they let you change the url and others do not. So the fastest thing has been in the HTML to create a script that in case you try to load the HTML from any page other than the login, go directly to the login ... This has solved the problems.

    
answered by 11.07.2016 в 17:24