Home >Web Front-end >Front-end Q&A >javascript close button
JavaScript close button application and implementation
In web design, sometimes we need to add a close button in a pop-up window or modal box so that users can close the window at any time. This article will introduce how to use JavaScript to implement the close button function.
1. Use HTML code to create a close button
In HTML code, we can use the bb9345e55eb71822850ff156dfde57c8
tag to create a close button, the code is as follows:
<button onclick="closeWindow()">关闭窗口</button>
Among them, the onclick
attribute will trigger a function when the button is clicked. We need to define the closeWindow()
function in JavaScript to realize the function of closing the window.
2. Use the Window object to close the window
In JavaScript, we can use the Window object to close the current window or an open child window. The following are two implementation methods:
window.close()
method to close the current window: function closeWindow() { window.close(); }
Note: window The .close()
method can only close windows opened by JavaScript. This method will not work if the current window is opened by the user.
window.opener
property to close the parent window or the opened child window: function closeWindow() { window.opener=null; window.open('','_self'); window.close(); }
In this example, we first window.opener
Set to null
to ensure that the closed window will not automatically open its parent window again. Then we use the window.open()
method to open a blank window. The first parameter of this method is the URL to be opened. Since we do not need to open any URL, an empty string is passed in . The second parameter is the window name, _self
is used here, which means opening a new web page in the current window. Finally we use the window.close()
method to close the current window.
3. Implement the close button in jQuery
If we use jQuery to write JavaScript code, we can use the following two methods to implement the close button function:
window.close()
method to close the current window (same as above): function closeWindow() { window.close(); }
function closeWindow() { var windowEvent = jQuery.Event('beforeunload'); $(window).trigger(windowEvent); if (!windowEvent.isDefaultPrevented()) { window.close(); } }
In this example, we first define a simulated window closing event. Then use jQuery's trigger()
method to trigger the event, which will execute all handlers registered to the event. If the handler has no default behavior of blocking the event, the window.close()
method will be executed to close the current window.
Summary:
This article introduces how to implement the close button in JavaScript and jQuery. Whether you close the window via the window.close()
method or simulate a window close event, remember to clearly define your closing behavior in your code and inform the user of it.
The above is the detailed content of javascript close button. For more information, please follow other related articles on the PHP Chinese website!