Home > Article > Web Front-end > Use javascript to control the window size of the opened web page and how to close the window in html
Use javascript to control the window size of the opened web page and how to close the window in HTML
With the popularity of the Internet, web design is becoming more and more important. Many websites will design some pop-up windows to provide a better user experience. JavaScript is a very important part of web design. It can be used to control various operations on the web page, such as controlling the window size, closing the window, etc.
This article will introduce how to use JavaScript to control the size of the window that opens a web page, and describe in detail how to close the window in HTML.
1. Use JavaScript to control the window size of the opened web page
Embedding JavaScript in the web page can control the size, position and status of the window, etc. The following is a piece of code that uses JavaScript to open a new window:
window.open("http://www.example.com","_blank","width=500,height=500");
The "window.open" in the code can open a new window, and "http://www.example.com" is to be opened. URL, "_blank" means opening in a new window, and "width=500,height=500" is the size of the window.
In addition, you can also use other parameters to control the characteristics of the window, such as:
For example, the following code will open a new window and set it to be in the center of the screen while hiding the toolbar:
window.open("http://www.example.com","_blank","width=500,height=500,left="+((screen.width-500)/2)+",top="+((screen.height-500)/2)+",toolbar=no");
2. How to close the window in HTML
In addition to opening the window, sometimes you need to close the window. HTML provides the window.close() method, which can be used to close the current window. However, this method is not supported by all browsers, and some browsers will prompt the user before closing.
If you use the window.close() method in JavaScript to close the window, incompatibility will occur in some browsers, so it is recommended to use the following statement in the HTML page to close the window:
<input type="button" value="Close Window" onClick="window.close();">
This is because in HTML, if the user clicks a button or link, calling the window.close() method directly will be regarded as a "user event" by the browser, thus bypassing the browser's response to window.close() ), the window can be closed safely.
It should be noted that this method is only effective when used in an open window. Used in the browser's main window will be ignored.
Summary
JavaScript can be used to control various operations on web pages. This article introduces how to use JavaScript to control the size of the window that opens a web page, and details how to close the window in HTML. After reading this article, you should know how to better use JavaScript in web pages to improve user experience and provide smarter and more convenient services.
The above is the detailed content of Use javascript to control the window size of the opened web page and how to close the window in html. For more information, please follow other related articles on the PHP Chinese website!