JavaScript closes the current window Tips

WBOY
Release: 2023-05-12 10:21:07
Original
1261 people have browsed it

JavaScript is a popular programming language that is widely used in web development. When users complete a task or open a web page, sometimes they want to close the current window. In JavaScript, closing the current window requires using the window.close() method. However, it may be necessary to display a prompt window to the user before closing the window.

The prompt window can be created by using the alert(), confirm() or prompt() function. The alert() function displays a simple message box, the confirm() function displays a message box with "OK" and "Cancel" buttons, and the prompt() function displays a message box with an input box and "OK" and "Cancel" buttons. In JavaScript, these functions can be used with the window.close() method to create a user-friendly prompt for closing the window.

The following is a sample code that demonstrates how to use the alert() function to prompt the user to confirm closing the current window:

window.onbeforeunload = function() {
   return "您确定要关闭当前窗口吗?";
};
Copy after login

In this code, we use the window.onbeforeunload event to register a callback function . This function will be called when the user attempts to close the current window. The callback function returns a string prompting the user for confirmation to close the window. When the user clicks the "Close" button, the window will be closed.

The following is a sample code using the confirm() function to demonstrate how to display a confirmation message box:

window.onbeforeunload = function() {
   return confirm("您确定要关闭当前窗口吗?");
};
Copy after login

In this code, we use the confirm() function to display a confirmation message box with " Message box with OK and Cancel buttons. When the user clicks the "OK" button, the callback function returns true and the window will be closed. When the user clicks the "Cancel" button, the callback function returns false and the window will not be closed.

Finally, here is a sample code using the prompt() function to demonstrate how to prompt the user to enter information:

window.onbeforeunload = function() {
   var name = prompt("请输入您的名字:");
   if (name != null) {
      return "再见," + name + "!";
   } else {
      return null;
   }
};
Copy after login

In this code, we use the prompt() function to display a message with Message box with input box and "OK" and "Cancel" buttons. When the user clicks the "OK" button, the callback function will return a farewell message with the user's name. When the user clicks the "Cancel" button, the callback function will return null and the window will not be closed.

In all these examples, we use the window.onbeforeunload event to register the callback function. This event is triggered when the window is about to close and can be used to perform certain operations before closing the window.

To summarize, JavaScript provides multiple methods to close the current window and prompt the user for confirmation. Use the alert(), confirm() or prompt() function to make the prompt message more friendly and easy to understand. Using the window.onbeforeunload event allows us to perform certain operations before closing the window, such as cleaning up temporary data or saving user input.

The above is the detailed content of JavaScript closes the current window Tips. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!