最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin9PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far