Example of window. open usage in JavaScript, window. open usage
This article analyzes in detail the usage of window. open in JavaScript. Share it with you for your reference. The details are as follows:
Copy codeThe Code is as follows: <script LANGUAGE = "javascript">
Window. open ('page.html ', 'newwindow', 'height = 100, width = 400, top = 0, left = 0, toolbar = no, menubar = no, scrollbars = no, resizable = no, location = no, status = no') // write a row
</Script>
Parameter description:
The js script starts;
Window. open command to pop up a new window;
'Page.html 'name of the pop-up window;
'Newwindow' indicates the name of the pop-up window (not the file name). It is optional and can be replaced by null;
Height = 100 window height;
Width = 400 window width;
Top = the pixel value between the 0 window and the top of the screen;
Left = 0 the pixel value between the window and the left of the screen;
Toolbar = no indicates whether to display the toolbar. yes indicates display;
Menubar and scrollbars indicate the menu bar and scroll bar.
Resizable = no: whether to change the window size. yes: yes;
Location = no indicates whether the address bar is displayed. yes indicates yes;
Status = no whether to display the information in the status bar (usually the file has been opened), yes is allowed;
1. Function Control pop-up window
The following is a complete code.
<Html>
A function openwin () is defined here, and the function content is to open a window. There is no purpose before calling it. How to call it?
Method 1: A window pops up when the browser reads the page;
Method 2: A window pops up when the browser leaves the page;
Method 3: call with a connection: Open a window. Note: The "#" is a virtual connection.
Method 4: call with a button:
2. Close the pop-up window on a regular basis (some websites are displayed n seconds after successful registration to the page before registration, or jump to the page by yourself)
Next we will make some control over the pop-up window, and the effect will be better.
If we add a short piece of code to the pop-up page (html is added to the HTML of page.html, not the homepage, otherwise...), will it be cool to enable it to be automatically disabled in 10 seconds?
First, add the following code to the page.html file:
<Script language = "Javascript"> function closeit () {setTimeout ("self. close ()", 10000) // millisecond} </script>
Then, use this phrase to replace the original sentence in page.html.
(Do not forget to write this sentence! The function of this statement is to call the code for closing the window. The window will be closed in 10 seconds.
3. Only one window is displayed (cookie control)
Recall that the pop-up window above is cool, but it has a slight problem. For example, you put the above script in a page that requires frequent passing (such as the homepage ),
So every time you refresh this page, the window will pop up. Is it very annoying? Is there a solution?
We use cookies for control.
First, add the following code to the HTML area of the Home 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, use it (not openwin, but loadpop !) Replace original on the home page
This sentence is enough. You can refresh the page or enter the page again, and the window will no longer pop up.
I hope this article will help you design javascript programs.