How do you close the current window in Layer?

In web development, JavaScript provides the window.close() method to close the current window. This method can only be used to close windows opened by JavaScript, not those manually opened by the user, for security reasons to prevent malicious websites from forcibly closing the user’s browser window.

To close the current window using the window.close() method, you can place it inside an event handler, such as a button’s click event.

<button onclick="closeWindow()">关闭窗口</button>

<script>
function closeWindow() {
    window.close();
}
</script>

When the user clicks the button, the closeWindow() function will be called to close the current browser window. Make sure this code is executed during user interaction to comply with the browser’s restrictions on automatically closing windows.

Leave a Reply 0

Your email address will not be published. Required fields are marked *