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

家住魔仙堡 提交于 2020-03-11 12:59:18

问题


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

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

回答1:


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!




回答2:


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.

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


来源:https://stackoverflow.com/questions/31765353/ie11-javascript-error-script445-object-doesnt-support-this-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!