Close current page/close current window in js (compatible with all browsers)

Source: Internet
Author: User

In js, the window or page cannot be judged. close () function, but if you want to be compatible with all browsers to close the current window, you do not directly use window. close.

Windows built-in Methods

Window. close ()

The Code is as follows: Copy code

<A href = "javascript: self. close ()"> close the window </a>

There are many problems with this method, so we will not discuss it much if it is incompatible. Let's look at another instance.

Window. close (), but it will prompt "the webpage you are viewing is trying to close the window. Close the window ?", How can I not bring up this prompt?
"If you open this webpage for the first time, window. close () will pop up a prompt, if this page is from another web page window. open () page, window. close (), no prompt will appear

The window does not display the js Code that is automatically closed.

The Code is as follows: Copy code


// The error message "close" appears in the normal mode.
Function closeie (){
Window. close ();
}
// Disable IE6. No prompt
Function closeie6 (){
Window. opener = null;
Window. close ();
}
// Disable IE7. No prompt
Function closeie7 (){
Window. open ('', '_ top ');
Window. top. close ();
}

However

Ff cannot be used anymore. Later, I found a function that is compatible with all browsers to close the current page.

The Code is as follows: Copy code

Function CloseWebPage (){
If (navigator. userAgent. indexOf ("MSIE")> 0 ){
If (navigator. userAgent. indexOf ("MSIE 6.0")> 0 ){
Window. opener = null;
Window. close ();
} Else {
Window. open ('', '_ top ');
Window. top. close ();
}
}
Else if (navigator. userAgent. indexOf ("Firefox")> 0 ){
Window. location. href = 'about: blank ';
} Else {
Window. opener = null;
Window. open ('', '_ self ','');
Window. close ();
}
}

Related Article

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.