OK, I looked a lot for this on the web but cannot find an answer.
I can expect CSS differences between browsers but there are JavaScript differences too?
So why this works in IE8:
window.print(); // works
but when I pass window.print
to a function and call it, it don't work in IE8 (works in IE9):
function callIt(f){
f.call();
};
callIt(window.print);
Is it a known issue?
EDIT
OK it does not work means it will simply ignore it, no javascript error or anything.
Sorry it gives this error:
Object doesn't support this property or method
EDIT 2
I need to use call
or apply
since I need to pass the context. I am trying to create a class which I can pass functions and it can call it with the possibility of passing context or arguments. Do not tell me to use f()
that is not an answer since it does not fix my problem. The question is on call
and apply
.
OK, I looked a lot for this on the web but cannot find an answer.
I can expect CSS differences between browsers but there are JavaScript differences too?
So why this works in IE8:
window.print(); // works
but when I pass window.print
to a function and call it, it don't work in IE8 (works in IE9):
function callIt(f){
f.call();
};
callIt(window.print);
Is it a known issue?
EDIT
OK it does not work means it will simply ignore it, no javascript error or anything.
Sorry it gives this error:
Object doesn't support this property or method
EDIT 2
I need to use call
or apply
since I need to pass the context. I am trying to create a class which I can pass functions and it can call it with the possibility of passing context or arguments. Do not tell me to use f()
that is not an answer since it does not fix my problem. The question is on call
and apply
.
- What behavior do you see? is there an error in the error console? – Sean McMillan Commented Jul 29, 2011 at 13:32
- Maby I misunderstood you but yes, there are A LOT of cross-browser differences in javascript. Check this thread: stackoverflow./questions/565641/… or google for more. That is why JS frameworks like jQuery are so popular ) – XzKto Commented Jul 29, 2011 at 13:35
- It gives me a javascript error in IE8 "Object doesn't support this property or method" – James Montagne Commented Jul 29, 2011 at 13:42
- Exactly! In my actual code, it gives me that but using this, it was not. – Aliostad Commented Jul 29, 2011 at 13:59
-
@Aliostad @XzKto This is not a JavaScript difference. The ECMAScript specification does not cover the behavior of host objects (which
window
andprint
are). A browser can arbitrarily define the behavior of its host objects - JavaScript (the language) is not related to that. This is an issue with the object model of IE, not a JavaScript issue. – Šime Vidas Commented Jul 29, 2011 at 14:07
3 Answers
Reset to default 10It seems window.*
functions are separate types than user-created functions in IE < 9. Thus, they don't get any of the Function.prototype.*
. You'll see that
typeof alert === 'object'
function a(){}
typeof a === 'function'
This would happen for any of the window.*
functions. Only for IE < 9. WTG Miscrosoft.
However you can try
Function.prototype.call.call(window.print)
See if that works for you.
function callIt(f) {
if (f) f();
}
callIt(window.print);
Done, no?
Update
per the poster's request that I answer the question, not remend a solution that works, here she goes:
If you view typeof(window.print) in IE, you'll see that it reports itself as type object. Type object has no apply or call method. In my opinion, your design is wrong for the task. HOWEVER, if what you want is a rabbit hole to follow, here's the top:
var p = window.print;
window.print = function() { p(); }
function callIt(f){
f.call();
}
callIt(window.print);
I have no idea what will happen in any other browser or how many procedural exceptions you'll have to make to account for it everywhere you'll need to.
You almost certainly should not be using .call()
here. f()
will call the method, while f.call()
will call it with an unset this
. Under es3 (but not es5 strict,) an undefined value for this
will be coerced to window
. I suspect that IE9 properly handles this, while IE8 does not, but that's just a guess based on behavior.
If print
cares about the value of this
, you should call it as window.print()
in order for this
to be set correctly. In that case, you may have to wrap it in an anonymous function so that print
doesn't get "sliced off" of window. callIt(function() { window.print();});