Javascript closes the current webpage code (compatible with all browsers)

Source: Internet
Author: User

In js, the most common method for closing a webpage is window. close () and window. the open () function is used in combination. Next I will introduce a variety of code for disabling web pages.

1. js code without any prompt to close the window

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 () does not appear

The Code is as follows: Copy code

<A href = "javascript: window. opener = null; window. open ('', '_ self'); window. close (); "> close </a>

2. Disable the custom prompt

The Code is as follows: Copy code

<Script language = "javascript">
Function custom_close (){
If (confirm ("are you sure you want to close this page? ")){
Window. opener = null;
Window. open ('', '_ self ');
Window. close ();
}
Else {}
}
</Script>

// This script is General for ie6 and ie7.

The Code is as follows: Copy code

<Input id = "btnClose" type = "button" value = "close this page" onClick = "custom_close ()"/>

3. close the current page: <a href = "javascript: window. opener = null; window. close ();"> close </a>. If it is a button:

The Code is as follows: Copy code

Response. Write ("<script language =" javascript "> window. opener = null; window. close (); </script> ");


// Close the current page and open a new page (no prompt)

The Code is as follows: Copy code

Function closeWinAndOpen (url ){

Var sWinName = "LR" + parseInt (Math. random () * 100000000); // use a random number to process WinName

Window. open (url, sWinName, 'toolbar = no, location = no, directories = no, status = yes, menubar = no, scrollbars = no, resizable = yes, copyhistory = yes ');

CloseWin ();

}

// Close the current page

The Code is as follows: Copy code

Function closeWin (){

Window. opener = null;

Window. open ('', '_ self ');

Window. close ();

}

Compatible with methods for disabling Web pages in all browsers

The Code is as follows: Copy code

<Script type = "text/javascript">
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 '; // by default, Firefox does not use window. close, which is not in the window. open status.
// Window. history. go (-2 );
}
Else {
Window. opener = null;
Window. open ('', '_ self ','');
Window. close ();
}
}
</Script>

Determine the js Code of each Browser:

The Code is as follows: Copy code

 

<Script language = "JavaScript">
<! --
Function getOs ()
{
Var OsObject = "";
If (navigator. userAgent. indexOf ("MSIE")> 0 ){
Return "MSIE ";
}
If (isFirefox = navigator. userAgent. indexOf ("Firefox")> 0 ){
Return "Firefox ";
}
If (isSafari = navigator. userAgent. indexOf ("Safari")> 0 ){
Return "Safari ";
}
If (isCamino = navigator. userAgent. indexOf ("Camino")> 0 ){
Return "Camino ";
}
If (isMozilla = navigator. userAgent. indexOf ("Gecko/")> 0 ){
Return "Gecko ";
}

}
Alert ("your browser type is:" + getOs ());
-->
</Script>
 

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.