


Detailed explanation of window.open usage examples in JavaScript_javascript skills
This article analyzes the usage of window.open in JavaScript in more detail. Share it with everyone for your reference. The details are as follows:
Parameter explanation:
js script starts;
window.open command to pop up a new window;
'page.html' file name of the pop-up window;
'newwindow' The name of the pop-up window (not the file name), optional, can be replaced by empty '';
height=100 window height;
width=400 window width;
top=0 pixel value above the window from the screen;
left=0 pixel value of the window from the left side of the screen;
toolbar=no whether to display the toolbar, yes means display;
menubar, scrollbars represent menu bars and scroll bars.
resizable=no Whether to allow changing the window size, yes means it is allowed;
location=no whether to display the address bar, yes is allowed;
status=no whether to display the information in the status bar (usually the file has been opened), yes is allowed;
1. Use functions to control pop-up windows
Below is a complete code.
<html> <head> <script LANGUAGE="Javascript"> <!-- function openwin() { window.open("page.html", "newwindow", "height=100,width=400, toolbar=no , menubar=no, scrollbars=no,resizable=no, location=no, status=no") //写成一行 } //--> </script> </head> <body onload="openwin()"> ...任意的页面内容... /body> </html>
A function openwin() is defined here, and the function content is to open a window. It serves no purpose until it is called. How to call it?
Method 1: A pop-up window appears when the browser reads the page;
Method 2: Pop-up window when the browser leaves the page;
Method 3: Call with a connection: open a window Note: The "#" used is a virtual connection.
Method 4: Call with a button:
2. Close the pop-up window regularly (some websites will display n seconds to go to the page before registration after successful registration, or jump by themselves)
Next, we will perform some controls on the pop-up window, and the effect will be better.
If we add a small piece of code to the pop-up page (note that it is added to the HTML of page.html, not the main page, otherwise...), wouldn't it be cooler if it automatically closes after 10 seconds?
First, add the following code to the area of the page.html file:
<script language="Javascript"> function closeit() { setTimeout("self.close()",10000) //毫秒 } </script>
Then, use this sentence to replace the original sentence in page.html.
(Don’t forget to write this sentence! The function of this sentence is to call the code to close the window, and the window will be closed automatically after 10 seconds.
3. Only pop up the window once (cookie control)
Recall that although the above pop-up window is cool, there is a slight problem. For example, if you put the above script in a page that needs to be passed frequently (such as the homepage),
So every time you refresh this page, the window will pop up. Isn't it very annoying? Is there any solution?
We use cookies to control this.
First, add the following code to the HTML area of the main page:
<script> function openwin() {window.open("page.html","","width=200,height=200")} function get_cookie(Name) { var search = Name + "=" var returnvalue = ""; if (documents.cookie.length > 0) { offset = documents.cookie.indexOf(search) if (offset != -1) { offset += search.length end = documents.cookie.indexOf(";", offset); if (end == -1) end = documents.cookie.length; returnvalue=unescape(documents.cookie.substring(offset,end)) } } return returnvalue; } function loadpopup(){ if (get_cookie('popped')==''){ openwin() ; documents.cookie="popped=yes" ; } } </script>
Then, replace the original
in the main page with (note not openwin but loadpop!)This sentence will do. You can try refreshing the page or re-entering the page, and the window will never pop up again.
I hope this article will be helpful to everyone’s JavaScript programming design.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Methods to close an open window: 1. Use "window.close" to close the new window; 2. Use "window.location" to redirect; 3. Use "window.blur" and "window.focus"; 4. Use "window. .history" fallback; 5. Use "window.opener".
