1

I found many questions of this type, but none helped.

Most of them give solution of using following code:

window.onbeforeunload = null;

My issue is when I type something in search box and press enter, jQuery function run which redirect to another url. but before redirect alert box come -> "Do you want to leave this site? Changes that you made may not be saved."

Here is my jquery code

<input type="text" placeholder="Search" value="" id="SearchBox" />

    $('#SearchBox').keypress(function (e) {
            if (e.which == 13) {
                var search = $('#SearchBox').val();
                window.onbeforeunload = null;
                window.location.href = 'https://redirecturl';
                
            }
          });
Nimantha
  • 6,405
  • 6
  • 28
  • 69
manpreet singh
  • 276
  • 4
  • 9

2 Answers2

0

Maybe you can try that Disable alert();

It disable alert box by assigning an empty function.

EDIT

Apparently you can also override the alert function JavaScript: Overriding alert().

Just return an empty function like this

(function(proxied) {
  window.alert = function() {};
})(window.alert);
Mister Q
  • 370
  • 6
  • 20
  • QHero Thanks for reply. But i am not using any alert box – manpreet singh Aug 11 '17 at 09:48
  • 1
    @manpreetsingh , uhm, yes, your code def has an beforeunload event. You have to find it. Find the "onbeforeunload" event listeners and delete it from your js file. It might be inline in the HTML, you will have to find it. You can also try window.onbeforeunload = function(){ return false;}; – ptts Aug 11 '17 at 10:10
  • @ptts Good one! I'm not really familiar with windows.onbeforeunload. So that's why I tried to offer another solution :) – Mister Q Aug 11 '17 at 10:12
  • @QHero, no problem. The onbeforeunload listener can be quite useful, you could even make you page fade out etc. The main use is, when you fill forms and accidentally click to close the browser tab while filling a form, then it call the confirm/alert box to prevent you from leaving the page and thus, losing the inputs in the form fields. In this case, there must be a listener for this event calling the alert box . This bring me to the solution actually, will post in a second as answer. – ptts Aug 11 '17 at 10:18
0

Add this at the end of the JS file, every JS file, just in case you cannot find the onbeforeunload listener.

window.removeEventListener("beforeunload");

And check the HMTL for inline onbeforeunload, just browse the HTML file with ctrl+f.

That is it, this will do the job.

ptts
  • 1,022
  • 8
  • 18