最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

youtube - IE11 JavaScript (Error: SCRIPT445) "Object doesn't support this action" - Stack Overflow

programmeradmin3浏览0评论

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/player_api", function() {
    yt_int = setInterval(function(){
        if(typeof YT === "object"){
            initYT();
            clearInterval(yt_int);
        }
    },500);
});

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);
});
Share Improve this question edited Dec 30, 2017 at 8:43 Alexis Tyler 9396 gold badges32 silver badges50 bronze badges asked Aug 1, 2015 at 19:22 cycluxcyclux 1431 gold badge2 silver badges5 bronze badges 4
  • Is it possible that the error line is being run before the youtube player script is loaded? – Zack Argyle Commented Aug 1, 2015 at 19:32
  • Sure, very possible ;) I thought yt_int had to be declared before getScript uses it.. So do you mean switching positions of $.getScript() and var yt_int, yt_players={} would help? I just tried.. seems to work, problem is that its very sporadic (hard to debug) – cyclux Commented Aug 1, 2015 at 19:46
  • 1 Some kind of race condition is the only thing I can think of, maybe try using a setTimeout to offload execution of the init function. Instead of initYT(), setTimeout(initYT, 0). – Zack Argyle Commented Aug 1, 2015 at 19:49
  • So, after 30+ refreshes on 3 different systems setTimeout(initYT, 0) really seems to fix it :) Thanks a lot! How can I declare your comment as the correct answer? – cyclux Commented Aug 1, 2015 at 21:19
Add a comment  | 

2 Answers 2

Reset to default 9

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

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.

发布评论

评论列表(0)

  1. 暂无评论