Use javascript to control the window size of the opened web page and how to close the window in html

王林
Release: 2023-05-09 11:25:07
Original
712 people have browsed it

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");
Copy after login

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:

  • left: the distance between the left side of the window and the left side of the screen
  • top: the top of the window Distance from the upper side of the screen
  • toolbar: whether to display the toolbar
  • location: whether to display the address bar
  • menubar: whether to display the menu bar
  • scrollbars: Whether to display scroll bars
  • resizable: whether it is resizable

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");
Copy after login

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:

Copy after login

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!

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!