window.open usage examples in JavaScript _javascript tips

Source: Internet
Author: User

In this paper, the window.open usage in JavaScript is analyzed in detail. Share to everyone for your reference. Specifically as follows:

Copy Code code 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 line
</script>

Parameter explanation:

JS script started;

window.open the command to eject a new window;
The filename of the ' page.html ' pop-up window;
' NewWindow ' pop-up window name (not filename), not required, available null ' instead;
height=100 window height;
width=400 window width;
The pixel value of the Top=0 window from the top of the screen;
The pixel value of the left=0 window from the left side of the screen;
Toolbar=no whether the toolbar is displayed, yes for display;
Menubar,scrollbars represents the menu bar and scroll bar.
Resizable=no whether to allow changes to the window size, yes to allow;
Location=no whether the address bar is displayed, yes is allowed;
Status=no whether the information in the status bar is displayed (usually the file is open), yes to allow;

1. Control pop-up window with function

The following is a complete code.

 
 

A function Openwin () is defined here, and the function content is to open a window. There is no use before calling it. How to call it?

Method One: When the browser reads the page pop-up window;

Method two: Pop-up window when the browser leaves the page;

Method Three: Call with a connection: open a window Note: The use of "#" is a virtual connection.

Method Four: Use a button to invoke:

2, timed to close the pop-up window (some Web site registration after the successful display of n seconds to go to the registration of the page information, or their own jump)

Below we have some control of the pop-up window, the effect is better.

If we add a small piece of code to the pop-up page (note is added to the page.html HTML, not the main page, otherwise ...), let it automatically shut down in 10 seconds is it cooler?

First, add the following code to the page.html file's area:

<script language= "Javascript" >
function Closeit ()
{
settimeout ("Self.close ()", 10000)//MS
}
</script>

Then, use this sentence to replace page.html Zhongyuan Some of this sentence on it.

(Don't forget to write this sentence!) The function of this sentence is to call the code that closes the window, and then close the window itself after 10 seconds.

3. pop-up window only (cookie control)

Recall that the pop-up window above is cool, but there's a bit of a problem, like you put the above script in a page that needs to go through a lot (such as the homepage),

So every time you refresh this page, the window will pop up once, is it annoying? Is there any way to solve it?

We use cookies to control.

First, add the following code to the area of the home page HTML:

<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 (note is not Openwin but Loadpop Ah!) Replace the main page in the central

This sentence can be. You can try to refresh the page or re-enter the page, and the window will never pop again.

I hope this article will help you with your JavaScript programming.

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.