IE11 JavaScript(错误:SCRIPT445)“对象不支持此操作". [英] IE11 JavaScript (Error: SCRIPT445) "Object doesn't support this action"

查看:930
本文介绍了IE11 JavaScript(错误:SCRIPT445)“对象不支持此操作".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Javascript解决方案来异步加载youtube播放器API. 整个脚本应在滚动到其位置时播放视频. 它适用于所有浏览器以及IE(11),但是有时在IE中我在开发人员工具中出现错误: SCRIPT445(对象不支持此操作).

I use a Javascript solution which loads the youtube player API asynchronously. The whole script is supposed to play the video when scrolled to its position. It works in all browsers and also in IE(11), but sometimes in IE I get an error in Developer Tools: SCRIPT445 (Object doesn't support this action).

Youtube Player仍然可以运行,但似乎使其他脚本崩溃.我在网上和Stackoverflow上四处张望.似乎还有其他人也有类似的问题,但是它们太具体了.也许有人可以帮我解决这个问题.这是导致问题的代码部分:

The Youtube Player still works but it seems to crash other scripts. I looked around in the web and also here on Stackoverflow. There seem to be others who have similar problems but they were too specific. Maybe someone could help me with this one. Here is the part of the code which makes the problem:

var yt_int, yt_players={},
    initYT = function() {
        $(".ytplayer").each(function() {
            yt_players[this.id] = new YT.Player(this.id);    <-- Error line 
        });
    };

$.getScript("//www.youtube.com/player_api", function() {
    yt_int = setInterval(function(){
        if(typeof YT === "object"){
            initYT();
            clearInterval(yt_int);
        }
    },500);
});

推荐答案

IE中存在一个竞争条件,该条件会在评估整个脚本之前触发脚本加载器回调.通过使用setTimeout(initYT, 0),您将允许脚本在启动初始化函数之前完成评估.很高兴!

There is a race condition in IE that is firing off your script loader callback before the entire script is evaluated. By using setTimeout(initYT, 0) you will allow the script to finish evaluating before firing your initialization function. Glad it worked!

这篇关于IE11 JavaScript(错误:SCRIPT445)“对象不支持此操作".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆