Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why doesn't youtube api javascript code work inside jquery document ready? [closed]

Youtube Javascript API code outside jQuery ready function: http://jsbin.com/umituf/1

Inside jQuery ready function: http://jsbin.com/umituf/2

I would like to use jQuery because when the video is stopped (even === 0) and loadmore() method is called, I want to write a ajax call in jQuery to go back to the server and fetch more video id's so they can be further passed to the cuePlaylist(...) method.

Why am I not able to mix jQuery and the API and How can it be done?

like image 987
Anthony Avatar asked Feb 18 '12 21:02

Anthony


People also ask

Is jQuery document ready deprecated?

on( "ready", handler ) , deprecated as of jQuery 1.8 and removed in jQuery 3.0.

Why are jQuery methods written inside a document ready event?

jQuery ready() Method The ready event occurs when the DOM (document object model) has been loaded. Because this event occurs after the document is ready, it is a good place to have all other jQuery events and functions. Like in the example above. The ready() method specifies what happens when a ready event occurs.

Is it necessary to use document ready in jQuery?

A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

What is difference between $( document .ready function () vs $( function ()?

The key difference between $(document). ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc.


1 Answers

The functions need to be defined in the global scope.

http://jsbin.com/umituf/3

Edit for clarification:
The youtube api script looks for those particular functions on the global scope. The api script does not have access to the scope that is created within the document.ready callback.

like image 137
Kevin B Avatar answered Oct 02 '22 06:10

Kevin B