0

I am trying to reload a previous page with data from the server when the user clicks on the back button. I am using the Window Before Unload method. The Ajax call is supposed to invoke the Controller and get me the values from the server but it is not firing.

Any help will be much appreciated.

$(function() {
     $(window).bind('beforeunload', function() {
         $.ajax({
             url: "/test/searchUrl",
             type: "GET",
             cache: false,
             datatype: "text",
             success: function (result) {
                 alert('HI Ajax Call SUCCESS' + result);
             }
         });
         return;
        };
     });
Macky
  • 433
  • 2
  • 9
  • 22

2 Answers2

0

I think maybe because the call is asynchronous the page is unloading before the ajax call completes. try making the ajax call synchronous which means it will hold there till it completes.

    $(function() {
         $(window).bind('beforeunload', function(){
                 $.ajax({
                     async: false,
                     url: "/test/searchUrl",
                     type: "GET",
                     cache: false,
                     datatype: "text",
                     success: function (result) {
                         alert('HI Ajax Call SUCCESS' + result);
                     }
                 });
                return;
            });
    });

EDIT: Fixed the unevent curly brackets and parentheses

Also, it seems that chrome blocks alerts in the beforeunload, assumingly to stop a developer from preventing you from leaving the page - you can however as the user above suggested return a string which will allow for a confirmation dialog with that string.

m.t.bennett
  • 1,290
  • 16
  • 34
  • Have you tried moving the alert outside of the ajax function? – m.t.bennett May 09 '13 at 01:44
  • Tried it without the alert but still no change. – Macky May 09 '13 at 01:49
  • Your brackets aren't matching up, your ending one is matching up to the start of the bind - i will edit mine and fix that – m.t.bennett May 09 '13 at 01:50
  • With the brackets fixed, it seems that chrome is blocking the 'alert' function in the beforeunload - this maybe a precaution to stop popups from preventing leaving the page – m.t.bennett May 09 '13 at 01:56
  • hmm..thanks. @m.t bennett Which brackets aren't matching?I can't seem to make out. – Macky May 09 '13 at 02:00
  • The original jquery object call wasn't closed off property, and there was a semi-colon in the wrong place after closing off the beforeunload function - try copy and pasting my solution see if that works :) - also in your demo returning 'BINGO' this was the same problem (not closed off). – m.t.bennett May 09 '13 at 02:02
  • Thank you @m.t bennett. You are correct. It was these problems that were preventing the code from firing. All good now !! – Macky May 09 '13 at 02:11
0

This question looks like it answers your question, Dialog box runs for 1 sec and disappears?. You are returning nothing from your beforeunload function, so it is not blocked and it leaves the page before the ajax call returns, you must return a string from the method that the browser will display.

Community
  • 1
  • 1
bigcakes
  • 216
  • 2
  • 9
  • $(function() { $(window).bind('beforeunload', function() { return "BINGO"; }; }); is not firing – Macky May 09 '13 at 01:53