In answering my question Pumbaa80 found a difference between calling open()
and window.open()
, try the following examples in Firefox (tested on 11.0):
http://jsfiddle.net/9kqp5/ (calls open
; opens in new tab in FF, provided that the "Open new windows in new tab instead" setting is on, which it is by default)
http://jsfiddle.net/HLbLu/ (calls window.open
; opens in new small window)
But why on earth there is a difference? If I try the following example:
<script>
var a = 2;
function hello() { alert(this.a); }
hello();
window.hello();
</script>
Both variants of calling function hello
work exactly the same, including having the same this
!!!
One of your fiddles is calling window.open
while the other is calling document.open
, because the scope chain in inline attribute event handlers is weird. So you end up at http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#dom-document-open
That said, since you pass 3 arguments, this should be invoking window.open
. The difference in behavior seems to be a bug in Firefox. I filed https://bugzilla.mozilla.org/show_bug.cgi?id=741266 on that.
Inside the event handler, open
by itself will resolve to document.open
. As Boris Zbarsky mentioned in a comment and in his answer, this is expected behavior, specified by HTML5. In the section on event handlers, step 6 specifies:
6. Using the script execution environment created above, create a function object (as defined in ECMAScript edition 5 section 13.2 Creating Function Objects), with:
(...)
Lexical Environment Scope
- Let Scope be the result of NewObjectEnvironment(the element's Document, the global environment).
- If the element has a form owner, let Scope be the result of NewObjectEnvironment(the element's form owner, Scope).
- Let Scope be the result of NewObjectEnvironment(the element's object, Scope).
(...)
In other words, variable references within the event handler will be resolved in the order:
document
propertiesYour two fiddles work the same for me on Chrome.
However, the two lines of code
window.open(...);
and
open(...);
are NOT equivalent. The only time they will be equivalent is if your current executing scope does not provide a new definition for open
, causing the interpreter to look in the higher scopes until it reaches the global scope and finds window.open
.
You can see this in action in this fiddle:
var test = function () {
var open = function () {
alert('uh oh');
};
window.open('www.google.com');
open('www.google.com');
};
test();
The are in fact the same. Try window.open === open
or window["open"] === open
.
If that yields false to you then you must be in a closure and somecode has defined open.
And of course this stands for all the objects that are member of the global (window) object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With