JavaScript windows. open () parameter set

Source: Internet
Author: User

[1. The most basic pop-up window Code]

Copy to ClipboardReference: [www.bkjia.com] <script language = "javascript">
<! --
Window. open ('page.html ')
-->
</SCRIPT>

Because it is a piece of javascripts code, they should be placed between the <script language = "javascript"> tag and </script>. <! -- And --> are used for some earlier browsers, and the code in the labels is not displayed as text in these old browsers. This is a good habit. Window. open ('page.html ') is used to control the pop-up window page.html. For example, if page.html is not in the same path as the main window, the path, absolute path (http: //) and relative path (../) should be specified in the front. Use single quotes and double quotes, but do not mix them. This code can be added to any location of HTML, and can be executed between  
[2. configured pop-up window]
  
Let's talk about the configuration in the pop-up window. You just need to add something to the code above. We will customize the appearance, size, and position of the pop-up window to adapt to the specific situation of the page.

Copy to ClipboardReference: [www.bkjia.com] <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 = n o, status = no') // write a line
-->
</SCRIPT>


Parameter description:

<Script language = "javascript"> the js SCRIPT starts;
Window. open command to pop up a new window;
'Page.html 'indicates the document name in the pop-up window;
'Newwindow' indicates the name of the pop-up window (not the document 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 document has been opened), yes is allowed;
</SCRIPT> end of js SCRIPT

[3. Function Control pop-up window]
  
The following is a complete code.

Copy to ClipboardReference: [www.bkjia.com] <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 ") // write a row
}
// -->
</Script>
</Head>
<Body onload = "openwin ()">
Any page content...
</Body>
</Html>

A function openwin () is defined here, and the function content is to open a window. It has no purpose before calling it. How to call it?

Method 1: <body onload = "openwin ()"> A window pops up when the browser reads the page;
Method 2: <body onunload = "openwin ()"> A window pops up when the browser leaves the page;
Method 3: call with a connection:

Copy to ClipboardReference: [www.bkjia.com] <a href = "#" onclick = "openwin ()"> open a window </a>

Note: "#" is a virtual connection.
Method 4: call with a button:

Copy to ClipboardReference: [www.bkjia.com] <input type = "button" onclick = "openwin ()" value = "open window">

4. Two windows are displayed at the same time]
  
Slightly changed the source code:

Copy to ClipboardReference: [www.bkjia.com] <script LANGUAGE = "javascript">
<! --
Function openwin (){
Window. open ("page.html", "newwindow", "height = 100, width = 100, top = 0, left = 0, toolbar = no, menubar = no, scrollbars = no, resizable = no, location = n o, status = no ") // write a row
Window. open ("page2.html", "newwindow2", "height = 100, width = 100, top = 1 00, left = 100, toolbar = no, menubar = no, scrollbars = no, resizable = no, loca tion = no, status = no ") // write a row
}
// -->
</Script>

To avoid overwriting the two pop-up windows, use top and left to control the position of the pop-up window. Finally, use the four methods mentioned above to call.
Note: The names of the two windows (newwindows and newwindow2) must not be the same, or they are all empty.

Open the file 1.htmin the main window, and pop up the window page.html at the same time]

Add the following code to the Copy to ClipboardReference: [www.bkjia.com] <script language = "javascript">
<! --
Function openwin (){
Window. open ("page.html", "", "width = 200, height = 200 ")
}
// -->
</Script>

Add to the <body> area:
<A href = "1.htm"> open </a>.
[6. timed close control for the pop-up window]
  
Next we will make some control over the pop-up window, and the effect will be better. If we add a small piece of code to the pop-up page (the page is added to the HTML of page.html, not the page, otherwise...), will it be cool to enable it to be automatically closed 10 seconds later?
First, add the following code to the Copy to ClipboardReference: [www.bkjia.com] <script language = "javascript">
Function closeit ()
{
SetTimeout ("self. close ()", 10000) // millisecond
}
</Script>

Then, use the <body> statement to replace the original <BODY> 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 .)
7. Add a close button in the pop-up window]

Copy to ClipboardReference: [www.bkjia.com] <FORM>
<Input type = 'button 'value = 'close'>
</FORM>

Perfect now!

[8. included pop-up window-one page and two windows]

The preceding example contains two windows: one is the main window and the other is the small window that appears. Through the example below, you can complete the above effect on a page.

Copy to ClipboardReference: [www.bkjia.com] <Head>
<Script language = "javascript">
Function openwin ()
{
OpenWindow = window. open ("", "newwin", "height = 250, width = 250, toolbar = no, scrollbars =" + scroll + ", menubar = no ");
// Write a row
Opendeskdoc ument. write ("<TITLE> example </TITLE> ")
Openmediaworkflow Doc ument. write ("<body bgcolor = # ffffff> ")
Openmediaworkflow Doc ument. write ("Openmediaworkflow Doc ument. write ("New window opened! ")
Openmediaworkflow Doc ument. write ("</BODY> ")
Openmediaworkflow Doc ument. write ("</HTML> ")
Openmediaworkflow Doc ument. close ()
}
</SCRIPT>
</Head>
<Body>
<A href = "#"> open a window </a>
<Input type = "button" value = "open window">
</Body>
</Html>

Do you think the code in openmediaworkflow Doc ument. write () is not standard HTML? You only need to write more rows according to the format. An error occurs when you add one or more labels. Remember to end with openmediaworkflow Doc ument. close.

[9. Ultimate application-Cookie control in the pop-up window]

In retrospect, although the pop-up window above is cool, there is a little bit of a problem (immersed in joy, do not find it ?) For example, if you place the above script in a page that requires frequent access (such as the homepage), the window will pop up every time you refresh the page, isn't it very annoying? :-(
Is there a solution? Yes! ;-) Follow me. We can use cookies to control it.
First, add the following code to the <HEAD> area of the HTML homepage:

Copy to ClipboardReference content: [www.bkjia.com] <script>
Function openwin (){
Window. open ("page.html", "", "width = 200, height = 200 ")
}
Function get_cookie (Name ){
Var search = Name + "="
Var returnvalue = "";
If (events. 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 (events. cookie. substring (offset, end ))
}
}
Return returnvalue;
}
Function loadpopup (){
If (get_cookie ('popped') = ''){
Openwin ()
Documents. cookie = "popped = yes"
}
} // Bkjia.com
</Script>

Then, use <body> (note that it is not openwin but loadpop !) Replace the original <BODY> sentence on the home page. You can refresh the page or re-enter the page. The window will no longer pop up. Real Pop-Only-Once!

The creation and application skills of the pop-up window are basically completed!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.