0

I have a variable "mail" inside "onPlayerReady" in which i have email adress, i want to call this variable inside "onPlayerStateChange" so that i can pass the value to 'input.php' page to save it into database.

$(function(){
  var youtubePlayer;
  var myWindow = window;
  var vid_id='ha48nSKLLh8';
  onYouTubeIframeAPIReady = function(){
   youtubePlayer = new YT.Player('youtube', {
     height: '390',
     width: '640',
     videoId: vid_id,
     events: {
       'onReady': onPlayerReady,
       'onStateChange': onPlayerStateChange
     }
   });
   }

  onPlayerReady = function(event) {
   event.target.pauseVideo();
   var mail=prompt('email:'); //this is the variable mail i am talking about i want the value if this variable inside onPlayerStateChange


   };


  onPlayerStateChange = function(event) {

   if (event.data == 1) {
       var tm = youtubePlayer.getCurrentTime();
       var st = youtubePlayer.getPlayerState();
       //$("#status1").text(tm);    


       $("#status1").text(st);
       $.ajax({
        type: 'POST',
        url: 'input.php',
        data:{ tm: tm, vid_id: vid_id,mail:mail},   
        success:function(html){
           alert(done);

        }
    });

       //$("#status1").text(tm);

     $("#status").text("Playing!");
     myWindow.focus();
     myWindow.onblur = function() {
       $("#status").text("You went away!");
       youtubePlayer.stopVideo();
     };
   }

  };

  youtubeplayer.bind('play',function(){
      youtubeplayer.time(60);
      return this.unbind;
  });
})

please help me with this or suggest me if there is any other way to save the email entered at "onPlayerReady". Thanks in advance!

Rajeet
  • 37
  • 1
  • 8

2 Answers2

1

I would suggest to declare the mail variable inside your closure and NOT as s global if not necessary i.e:

$(function(){
    var mail = null; // not a global, but available in the entire function scope.
    var youtubePlayer;
    var myWindow = window;
    ...

The nuance is that now NO other code can potentially override the mail variable (or vice versa), therefore no naming collisions. It's best practice to avoid globals as much a possible in javascript.

Only make sure to NOT redefine you mail variable in onPlayerReady():

onPlayerReady = function(event) {
   event.target.pauseVideo();
   mail = prompt('email:'); // Only store the prompt input (i.e no var).
};

And this way you can still access the mail variable in you ajax request without the downsides mentioned above.

Also for reference check this post.

Community
  • 1
  • 1
danillouz
  • 6,121
  • 3
  • 17
  • 27
0

If you declare the mail variable outside the $(function(), it will be a global variable and accessible by all your Javascript code. E.g.

var mail = "";
$(function(){
    ...

    onPlayerReady = function(event) {
        event.target.pauseVideo();
        mail=prompt('email:'); // remove the var here
    };

In fact, as @nikhil pointed out, you can even put it inside the outer function() (as long as it is outside the onPlayerReady function).

Glorfindel
  • 21,988
  • 13
  • 81
  • 109