Home > Web Front-end > JS Tutorial > How to achieve the pop-up 'Are you sure you want to leave this page?' when chrome browser closes the page_javascript skills

How to achieve the pop-up 'Are you sure you want to leave this page?' when chrome browser closes the page_javascript skills

WBOY
Release: 2016-05-16 16:11:10
Original
5097 people have browsed it

1. Avoid pop-up prompts

I searched a lot on the Internet, and most of the answers were to set window.onbeforeunload=null, but it didn’t work after trying it out.

After I put this question away for two days, I came back and thought about it again, and finally found the answer. I would like to share it with you here:

Release jquery exit page pop-up prompt (1) Unbind first and set the pop-up content to null.

Copy code The code is as follows:

$(function(){
​​$(window).unbind('beforeunload');
​window.onbeforeunload = null;
})

2. Other related [Abstract]

(1)window’s onunload and onbeforeunload events

The following refers to the implementation in js, not the method of !
Because this is triggered when the body is unloaded, and any browser will unload the body when it is closed!

Model 1:

Copy code The code is as follows:

function close(){
alert("this is a test");
}
window.onbeforeunload=close;

Model 2:

Copy code The code is as follows:

function close(){
If(document.body.clientWidth-event.clientX< 170&&event.clientY< 0||event.altKey)
{
alert("this is a test");
}  
}
window.onbeforeunload=close;

Copy code
About model 1:
1). Refresh, suitable for both multi-window and single window.
2).Single window IE closes the entire IE trigger.
3). Close single page trigger in ie7 multi-window
4) Other multi-window refresh triggers. Closing a single window and closing the entire window will not trigger

For model 2:
1).ie single window and ie7 multi-window need to close the entire browser to trigger
2). Refresh of other multi-window browsers. Closing a single page or closing the entire page will not trigger

(2) Create a leave prompt box

Bind beforeunload event

Copy code The code is as follows:

$(window).bind('beforeunload',function(){
Return 'The content you entered has not been saved. Are you sure you want to leave this page? ';
});

Unbind

Copy code The code is as follows:

$(window).unbind('beforeunload');
window.onbeforeunload = null;

The above is all the content described in this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template