JavaScript 在 Internet Explorer 8(和 7)中调用并应用 window.print

发布于 2024-11-27 03:16:05 字数 753 浏览 0 评论 0原文

好吧,我在网上查了很多,但找不到答案。

我可以预期浏览器之间的 CSS 差异,但 JavaScript 也有差异?

那么为什么这在 IE8 中有效:

window.print(); // works

但是当我将 window.print 传递给函数并调用它时,它在 IE8 中不起作用(在 IE9 中有效):

function callIt(f){
    f.call();
};



callIt(window.print);

是吗?一个已知问题?


编辑

好吧,它不起作用意味着它会简单地忽略它,没有javascript错误或任何东西。

抱歉,它给出了这个错误:

   Object doesn't support this property or method

编辑2

我需要使用callapply 因为我需要传递上下文。我正在尝试创建一个可以传递函数的类,并且它可以通过传递上下文或参数的可能性来调用它。不要告诉我使用 f() 这不是答案,因为它不能解决我的问题。 问题在于通话申请

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬心 2024-12-04 03:16:05

看来 window.* 函数与 IE 中用户创建的函数是不同的类型。 9. 因此,他们没有获得任何Function.prototype.*。您将看到

typeof alert === 'object'

function a(){}

typeof a === 'function'

任何 window.* 函数都会发生这种情况。仅适用于 IE < 9.WTG微软。

不过,您可以尝试

Function.prototype.call.call(window.print)

看看这是否适合您。

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.

黒涩兲箜 2024-12-04 03:16:05
    function callIt(f) {
        if (f) f();
    }

    callIt(window.print);

完成了,不是吗?


根据发布者的要求进行更新

,我回答了这个问题,而不是推荐一个有效的解决方案,她在这里:

如果您在 IE 中查看 typeof(window.print),您会看到它将自己报告为类型对象。类型对象没有 apply 或 call 方法。在我看来,你的设计对于这项任务来说是错误的。但是,如果您想要的是一个可遵循的兔子洞,那么最重要的是:

var p = window.print;
window.print = function() { p(); }

function callIt(f){
     f.call();
}

callIt(window.print);

我不知道在任何其他浏览器中会发生什么,也不知道您需要在任何需要的地方做出多少程序异常来解释它。

    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.

断肠人 2024-12-04 03:16:05

几乎可以肯定,您不应该在这里使用 .call()f() 将调用该方法,而 f.call() 将使用未设置的 this 来调用该方法。在 es3 下(但不是严格的 es5),this 的未定义值将被强制为 window。我怀疑 IE9 可以正确处理这个问题,而 IE8 则不能,但这只是基于行为的猜测。

如果 print 关心 this 的值,您应该将其调用为 window.print() 以便 this > 设置正确。在这种情况下,您可能必须将其包装在匿名函数中,以便 print 不会被窗口“切掉”。 callIt(function() { window.print();});

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();});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文