Home >Web Front-end >Front-end Q&A >html close page
How to close a page using HTML
HTML is a markup language that is used to create web pages. In HTML, closing a page is a common task. In this article, we will discuss how to close a web page using HTML.
Close the page using JavaScript
In HTML, we can use JavaScript to close a page. To close the page, we need to use the close() function of the window object. The following is a simple example:
<!DOCTYPE html> <html> <head> <title>关闭页面</title> </head> <body> <button onclick="window.close()">关闭页面</button> </body> </html>
In the above code, we add a button to the page. When the user clicks the button, the close() function of the window object will be called to close the page.
Use links to close the page
In HTML, we can also use links to close the page. The following is an example:
<!DOCTYPE html> <html> <head> <title>关闭页面</title> </head> <body> <a href="#" onclick="window.close()">关闭页面</a> </body> </html>
In this example, we use a hyperlink to implement the function of closing the page. When the user clicks the link, the close() function of the window object is called to close the page.
In this example, we use "#" as the link address. This is because we don't want the link to navigate to another page or reload the current page. Instead, we just use the link to trigger JavaScript code to close the current page.
Close the page using metadata
In addition to JavaScript and links, HTML also supports using metadata to close the page. Here is an example:
<!DOCTYPE html> <html> <head> <title>关闭页面</title> <meta http-equiv="refresh" content="0;url=javascript:window.close()"> </head> <body> <h1>这是一个测试页面</h1> </body> </html>
In the above example, we have added the e8e496c15ba93d81f6ea4fe5f55a2244 element in the 93f0f5c25f18dab9d176bd4f6de5d30e tag. This element allows us to embed HTTP response header information into the HTML page.
In this example, we use the meta element to set a new HTTP response header. We set this response header to trigger JavaScript code to close the page immediately when the page loads.
The content attribute here specifies the value of the HTTP response header. This value consists of two parts. The first part "0" specifies the page wait time in seconds. We set it to 0 seconds, which means the page will be closed immediately.
The second part specifies the JavaScript code. We use "javascript:window.close()" to call the function that closes the current page.
Conclusion
In HTML, there are several ways to close a page. The most common method is to use JavaScript to close the page. However, you can also use links and metadata to achieve the same effect. No matter which method you use, it will help you close the page, thereby improving the user experience.
The above is the detailed content of html close page. For more information, please follow other related articles on the PHP Chinese website!