JavaScript 在 Internet Explorer 8(和 7)中调用并应用 window.print
好吧,我在网上查了很多,但找不到答案。
我可以预期浏览器之间的 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
我需要使用call
或apply
因为我需要传递上下文。我正在尝试创建一个可以传递函数的类,并且它可以通过传递上下文或参数的可能性来调用它。不要告诉我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来
window.*
函数与 IE中用户创建的函数是不同的类型。 9. 因此,他们没有获得任何
Function.prototype.*
。您将看到任何
window.*
函数都会发生这种情况。仅适用于 IE < 9.WTG微软。不过,您可以尝试
看看这是否适合您。
It seems
window.*
functions are separate types than user-created functions in IE < 9. Thus, they don't get any of theFunction.prototype.*
. You'll see thatThis would happen for any of the
window.*
functions. Only for IE < 9. WTG Miscrosoft.However you can try
See if that works for you.
完成了,不是吗?
根据发布者的要求进行更新
,我回答了这个问题,而不是推荐一个有效的解决方案,她在这里:
如果您在 IE 中查看 typeof(window.print),您会看到它将自己报告为类型对象。类型对象没有 apply 或 call 方法。在我看来,你的设计对于这项任务来说是错误的。但是,如果您想要的是一个可遵循的兔子洞,那么最重要的是:
我不知道在任何其他浏览器中会发生什么,也不知道您需要在任何需要的地方做出多少程序异常来解释它。
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:
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.
几乎可以肯定,您不应该在这里使用
.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, whilef.call()
will call it with an unsetthis
. Under es3 (but not es5 strict,) an undefined value forthis
will be coerced towindow
. 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 ofthis
, you should call it aswindow.print()
in order forthis
to be set correctly. In that case, you may have to wrap it in an anonymous function so thatprint
doesn't get "sliced off" of window.callIt(function() { window.print();});