简体   繁体   中英

javascript window.open doesn't work

I want to use window.open to open a window to one of my JSP file. But the browser keeps showing connecting.. . And even firebug stops working every time I click the text. Neither the p nor the input tags work, but when I use a href to link the JSP it can link to the file:

<!DOCTYPE html>
<html>
<head><title>Sample JSP Page</title>
<script>
function open(){
    //window.open("hello.jsp","hello","height=700, width=800");
    var x=window.open("hello.jsp","window","status=1,height=700, width=800");
    x.focus();
}
</script>
</head>
<body>
<h1>Sample JSP Page</h1>
<p onclick="open()">do not work</p>
<form>
<input type="button" value="new window" onclick="window.open('test-with-utils')"></form>
</body>
</html>

That's because you have redefined window.open when you defined the function open. Use a different function name instead.

更改功能的名称。

The window object is the top level object in JavaScript, and contains in itself several other objects, such as "document", "history" etc.

When you define a variable or a function of your own you really add a new property to the window object. And this will work ( and a little live example ):

var foo = "bar";
alert ( window.foo ); // says "bar"

In addition if you add this little snippet to your code:

window.onerror = function ( msg, url, num ) {
   alert ( "Error: " + msg + "\nURL: " + url + "\nLine: " + num );
   return true;
};

you will get this error, when press the button:

Error: Uncaught RangeError: Maximum call stack size exceeded

This means an endless recursion. It is a side effect - you define a new open function, and when you call window.open() , you recursively call your function.

Just to expand on the reason that you are having problems here, you may want to read a little about javascript Scope ( Very Helpful Blog ). Essentially, consider the following code:

<script>
var thisOne=true;
function thatOne() {
alert("whizbang");
}
var theOther={foo:"bar"};

//More code here...
</script>

Once you reach the comment, you know you can access those variables and the function directly, like if (thisOne) {...} , element.onclick=thatOne; or console.log(theOther.foo) . However, you can also access them as children of the root object which, in a web browser, is called window . So you can do:

console.log(window["thisOne"]);
window.thatOne.call(obj, params);
console.log(window.foo.bar);

so by defining open() as a function which is not inside another element (which is to say, is inside the root element), you overwrite the window.open() function. When you attempt to call the function later on, you get problems because the open function calls window.open, which calls window.open, which calls window.open...

There's a few ways to get around this -

Define the onclick handler inline

To do this, get rid of the whole <script>..</script> element then, using whichever element you choose (that supports it) add the onclick attribute:

onclick="window.open('hello.jsp','window','status=1,height=700, width=800');"

This is a nice and quick method, and it keeps all the logic right there with it's triggering element, but it is not easily extensible and you may find yourself sneered at by some. ("Oh, you use inline javascript? how quaint")

change the method name

This will take the least effort from you in terms of getting your page working now from what you have (it's also essentially what everyone else has suggested). Just change the name of the open method to something like openANewWindow() or gotoJSP() or anything that doesn't already exist in the root object, making sure to get both where you define it (in the script element) and where you use it (in the onclick attributes).

Use a closure

This is almost definitely not what you want in this case, its more complexity than you need for a single function. Just including this as an example of how to get out of the root object, seeing as being in it seems to be the heart of your problem.

You have probably already seen in javascript how to define an object, but you may not know that by defining an object, all you are really doing is adding an object property to the root object. You can use this behavior to your advantage, to give a hierarchical structure to your functions.

For example:

<script>
var MyFunctions = (function() {
    function open(){
    var x=window.open("hello.jsp","window","status=1,height=700, width=800");
    x.focus();
    }
    return {open:open};
})();
</script>

This creates an anonymous function that is immediately run. Inside the scope of this function, another function, open() is defined, however it is defined within the scope of that anonymous function, not the root object (window). After open() is defined, a reference to it is returned as the value of the object property: open.

The result of all this is that the open property of the MyFunctions object is the function you need. You can then call it with MyFunctions.open() or even window.MyFunctions.open().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM