Javascript determines window close events

Source: Internet
Author: User
  

1. the browser is not a software window. It is very difficult to identify the event because closing and refreshing events are difficult to distinguish.
2. There are no corresponding events, which can only be determined based on certain conditions, but may not be accurate.
3. forward, backward, refresh, and close all respond to event window. onbeforeunload.
4. The reason why javascript can obtain certain event attributes and call methods is that the browser object provides interfaces for it. In terms of COM, it is an idispatch interface, if the browser object does not provide interfaces for him, he cannot do anything. Therefore, some functions can be implemented in other languages, but cannot be implemented in JavaScript.

OnUnload Method
Add an onUnload Event to the body tag
Body onUnload = "myclose ()"
Then define the myclose () method in Javascript, but the onUnload method is executed after the window is closed, not before the window is closed. If you want to make a judgment before closing the window, please use the first method

  function  getevent () ///   compatible with both IE and FF writing, this method is online copy   
{< br> If (document. all) return window. event;
func = getevent. caller;
while (fun C! = null ) {
var arg0 = func. arguments [0];
If (arg0)
{< br> If (arg0.constructor = event | arg0.constructor = mouseevent)
| (Typeof(Arg0) = "object" & arg0.preventdefault & arg0.stoppropagation ))
{
ReturnArg0;
}
}
Func = func. Caller;
}
Return Null;
}

Function Confirmclose (){
If (Window. Event ){
Window. event. returnvalue = "Make sure you have saved the information before closing the window! ";
} Else {
Getevent (). preventdefault (); // For Firefox
}
}

Function On_page_loaded (){ // Onload event of the custom body
Try {
If (! Window. onbeforeunload ){ // In order not to overwrite the original onbeforeunload method, first judge
Window. onbeforeunload = confirmclose; // The prompt before the window is closed is added to todo.
}
} Catch (E ){}
}
<Body onload = "on_page_loaded ()">

 

// Determine whether the parent form is closed before opening the form

 
FunctionRsseek (){
If(Opener ){
If(Typeof(Registry.opener.doc ument) = 'unknown '|Typeof(Registry.opener.doc ument) = 'undefined '){
//Disabled
}Else{
Opener. myseekok ();
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.