IE11 JavaScript(错误:SCRIPT445)“对象不支持此操作”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31765353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:10:16  来源:igfitidea点击:

IE11 JavaScript (Error: SCRIPT445) "Object doesn't support this action"

javascriptyoutubeinternet-explorer-11

提问by cyclux

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 sometimesin IE I get an error in Developer Tools: SCRIPT445 (Object doesn't support this action).

我使用异步加载 youtube 播放器 API 的 Javascript 解决方案。整个脚本应该在滚动到其位置时播放视频。它适用于所有浏览器,也适用于 IE(11),但有时在 IE 中我会在 Developer Tools: SCRIPT445 (Object does not support this action) 中收到错误消息

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:

Youtube 播放器仍然有效,但它似乎使其他脚本崩溃。我在网上和 Stackoverflow 上环顾四周。似乎还有其他人有类似的问题,但他们太具体了。也许有人可以帮我解决这个问题。这是导致问题的代码部分:

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

采纳答案by Zack Argyle

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!

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

回答by Narpat Shekhawat

object doesn't support this action is error is coming in IE11 using window.dispatchEvent(new Event('resize')); we need to d handle the condition for ie11.

对象不支持此操作是使用 window.dispatchEvent(new Event('resize')); 在 IE11 中出现错误。我们需要处理ie11的条件。

 if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
                   var evt = document.createEvent('UIEvents');
                   evt.initUIEvent('resize', true, false, window, 0);
                   window.dispatchEvent(evt);
        } else {
                 window.dispatchEvent(new Event('resize'));
        }