Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7982715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:45:13+00:00 2026-06-04T10:45:13+00:00

I have the following code, necessary to track an embedded Youtube video in my

  • 0

I have the following code, necessary to track an embedded Youtube video in my page using their Javascript API:

function onYouTubePlayerReady(playerId) {
      var youtubeplayer = document.getElementById('youtubeplayer');
      youtubeplayer.addEventListener("onStateChange", "onytplayerStateChange");
      youtubeplayer.setPlaybackQuality('large');
      }

function onytplayerStateChange(newState) {
    //Make it autoplay on page load
    if (newState == -1) {
        youtubeplayer.playVideo();
        }

    var tiempo = youtubeplayer.getCurrentTime();
    //Rounds to 2 decimals
    var tiempo = Math.round(tiempo*100)/100;
    alert(tiempo);
    }

(The details related to the Youtube API aren’t important, but if you are curious, they are at my previous question
Youtube Javascript API: not working in IE).

Now, my problem is that the onYoutubePlayerReady() function declares “youtubeplayer”, which is a reference to a DOM object, and I need to access that variable from the other function onytplayerstateChange() too… but “youtubeplayer” is being declared as a local variable. This works in IE 8, Safari…, but doesn’t work in Firefox 12. Of course, I could get a reference to “youtubeplayer” in the following way instead, without the “var”:

youtubeplayer = document.getElementById('youtubeplayer');

And in fact, that is how it looks in Youtube’s sample code, but if I write it this way, the code doesn’t work in IE 8 (while it does work in Firefox 12; see my previous question for more details).

…All of which brings me to: is there a way to declare a global variable from inside a function (like “youtubeplayer” above), so that it can be accessed by other functions too? Something that works in all browsers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:45:14+00:00Added an answer on June 4, 2026 at 10:45 am

    Another way to declare globals is to create it as an extra property to the global “window” object– so do

    window.youtubeplayer = document.getElementById('youtubeplayer'); 
    

    This should be accessible to anything else on the page. So your complete code would look like this;

    function onYouTubePlayerReady(playerId) {
      window.youtubeplayer = document.getElementById('youtubeplayer');
      window.youtubeplayer.addEventListener("onStateChange", "onytplayerStateChange");
      window.youtubeplayer.setPlaybackQuality('large');
    }
    
    function onytplayerStateChange(newState) {
      //Make it autoplay on page load
      if (newState == -1) {
        window.youtubeplayer.playVideo();
      }
    
      var tiempo = window.youtubeplayer.getCurrentTime();
      //Rounds to 2 decimals
      var tiempo = Math.round(tiempo*100)/100;
      alert(tiempo);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code. ASPX Page <a href=AnyASPXPageOfWebsite.aspx onclick=javascript:CallJQuery(); > Set Price </a> JS
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for inserting data into database using PDO. It inserts data
I have following code using hibernate to throw a custom exception on error and
So I am using Silverlight 4.0 I have the following code but its giving
I am using google admob in my application. I have the following code: GADRequest
I have the following code: #include <iostream> using namespace std; class A { int
I have the following code: $(document).ready(function(){ $(.datepicker).each(function() { $(this).datepicker({ beforeShowDay: function(date){ return [$(this).parent().find(span.days).html(), ]
I have the following code on my script file: $(document).ready(function(){ $(#cancel_button).click(function(){ alert(TEST); }); });
Say I have the following code: #include <iostream> using namespace std; class Account {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.