JavaScript BOM Note window.open Example

Source: Internet
Author: User

Read for a long time, finally arrived I think the actual combat part, the first is window.open, plainly is the pop-up window.
In terms of the basics:

The code is as follows Copy Code
Equivalent to <a href= "http://111cn.net" target= "Topframe" ></a>
window.open ("Http://111cn.net", "topframe");

There is no future for equivalence, so there are other uses here, Topframe, which is the second argument that can pass in _self,_parent,top and _blank, while the second argument is not an existing window or frame, it reads the third parameter to determine The third parameter seems to be some of the less useful, mainly top,width,left,height position.

Like what:

The code is as follows Copy Code
window.open (' http://111cn.net ', ' _blank ', ' height=500, width=300, left=20, top=20 ');

A window pops up (but most of the time it's a harmonious fate)

In addition, the close () method closes the pop-up window (other windows are not authorized to operate), and moveto () ' Resizeto () is known by name.

If the window is blocked, it generally returns null and throws an error, so we can use

  code is as follows copy code
    var blocked = false;
    try {
        var pop = window.open (' http://111cn.net ', ' _blank ', ' height=500, width=300, left=20, top=20 ');
        if (pop = null) {
             blocked = true;
       }
   } catch (ex) {
        blocked = true;
  & nbsp
    if (blocked) {
        alert ("The pop was blocked");
   }

To detect if the window was blocked and prompted.

Then slightly with the knowledge of the previous toss a bit, specific in the source code can also write quite clear, code slag, do not blame.

Demonstrate

The code is as follows Copy Code

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title>Window-on</title>
<body>
<script>
function NewWindow () {
This.win = window.open (' http://111cn.net ', ' _blank ', ' height=500, width=300, left=20, top=20 ');
}
Newwindow.prototype = {
Constructor:newwindow,
Close:function () {
This.win.close ();
},
Resize:function () {
This.win.resizeTo (300, 300);
},
Moveto:function () {
This.win.moveTo (100, 100);
}
}
/* First way: Simple version
var pops = window.open (' http://111cn.net ', ' _blank ', ' height=500, width=300, left=20, top=20 ');
if (pop = = null) {
Alert ("The pop was blocked!");
}
*/
/* The second way: more accurate * *
var blocked = false;
try {
var pops = window.open (' http://111cn.net ', ' _blank ', ' height=500, width=300, left=20, top=20 ');
if (pop = = null) {
blocked = true;
}
catch (ex) {
blocked = true;
}
if (blocked) {
Alert ("The pop was blocked");
}
</script>
<input type= "button" value= "Hello Sky" onclick= "Windows = new NewWindow ();" />
<input type= "button" value= "Close" onclick= "windows.close ();"/>
<input type= "button" value= "resize" onclick= "windows.resize ();"/>
<input type= "button" value= "MoveTo" onclick= "Windows.moveto ();"/>
</body>

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.