Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Call and Apply in internet explorer 8 (and 7) for window.print

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.

like image 265
Aliostad Avatar asked Dec 12 '22 10:12

Aliostad


2 Answers

It 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.

like image 75
Richard Hoffman Avatar answered Dec 22 '22 01:12

Richard Hoffman


    function callIt(f) {
        if (f) f();
    }

    callIt(window.print);

Done, no?


Update

per the poster's request that I answer the question, not recommend 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.

like image 29
Brian Avatar answered Dec 22 '22 00:12

Brian