jquery - Why does this javascript work...but this doesn't? - Stack Overflow

I load everything on my page.At the end, I have this script.<script type="textjavascript"

I load everything on my page. At the end, I have this script.

<script type="text/javascript">loadNewVideo('u1zgFlCw8Aw',0)</script>

It doesn't work. But if I substitute it with this, it works.:

<input type="submit" onclick="loadNewVideo('u1zgFlCw8Aw',0);" >

In the .JS file, this is the function:

function loadNewVideo(id, startSeconds) {
            alert('In-the-function');
          if (ytplayer) {
            alert('Found-ytplayer');
            ytplayer.loadVideoById(id, parseInt(startSeconds));
          }
        }

Apparently, "ytplayer" is false in the first one!??

I load everything on my page. At the end, I have this script.

<script type="text/javascript">loadNewVideo('u1zgFlCw8Aw',0)</script>

It doesn't work. But if I substitute it with this, it works.:

<input type="submit" onclick="loadNewVideo('u1zgFlCw8Aw',0);" >

In the .JS file, this is the function:

function loadNewVideo(id, startSeconds) {
            alert('In-the-function');
          if (ytplayer) {
            alert('Found-ytplayer');
            ytplayer.loadVideoById(id, parseInt(startSeconds));
          }
        }

Apparently, "ytplayer" is false in the first one!??

Share Improve this question asked Oct 14, 2009 at 8:29 TIMEXTIMEX 273k368 gold badges802 silver badges1.1k bronze badges 3
  • 2 You should show the plete code. Where does ytplayer e from? – Peter Stuifzand Commented Oct 14, 2009 at 8:32
  • 3 Where is ytplayer defined and what is it? – Tim Down Commented Oct 14, 2009 at 8:35
  • If you can give us a link then we could look in Firebug, or you could... – Skilldrick Commented Oct 14, 2009 at 9:17
Add a ment  | 

3 Answers 3

Reset to default 9

I don't know what ytplayer is, but I imagine this problem has something to do with the DOM not being fully loaded. Have you tried this?

<script type="text/javascript">
$(document).ready(function() {
  loadNewVideo('u1zgFlCw8Aw', 0);
});
</script>

edit: If this doesn't work, you will have to give us more information. As Tim and Peter asked you in ments to your question, can you tell us where ytplayer is defined and what it is? I'm assuming it stands for YouTube Player, maybe it's being loaded/defined after you were calling loadNewVideo?

For example, if the code snippet above is above the part where ytplayer is defined in your document, this would still be called before ytplayer is loaded, depending on how you're loading it. As I said, please supply some more information.

I don't have an answer, but perhaps this will help you figure out when the ytplayer ponent is ready for use:

var startTimer = new Date().getTime();
function ytplayerReadyCheck() {
  var ready = !!ytplayer;
  if (!ready) {
    window.setTimeout(ytplayerReadyCheck,50);
  }
  else {
    var endTimer = new Date().getTime();
    alert('ytplayer is now ready!\n\nWe waited '+(endTimer-startTimer)+' milliseconds for it');
    loadNewVideo('u1zgFlCw8Aw', 0);
  }
}
ytplayerReadyCheck();

Of course, this is scarcely replacement for figuring out where ytplayer is defined in the first place! Perhaps it's defined in a .js file that's dynamically loaded after the document has loaded?

I'd highly remend getting hold of Firebug so you can more easily debug this kind of thing.

I would say this is because the DOM hasn't totally loaded at that stage. You want to hook the body onLoad event, or use jQuery to get the $(document).ready() hook.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745595273a4635074.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信