1

Youtube's Videobar is an outdated widget. Google is still serving the js but the documentation and links have been taken off from their site. You can see a running example of this over at: sicmaui.com (right side, youtube tab)

Its currently being loaded using a script tag on html

<script src="http://www.google.com/uds/api?file=uds.js&v=1.0&source=uds-vbw" type="text/javascript"></script>
GSearch.setOnLoadCallback(somefunction); //works

But it doesn't work when I try to load this using the following code

var $script = $('<script></script>').attr('src', src).bind('load', function(){
GSearch.setOnLoadCallback(somefunction); //GSearch is undefined
});
$('head')[0].appendChild($script[0]);

Now the js is being loaded perfectly fine. But the variable is undefined. I digged into the google's js and found that later on its loading another js file using this code:

google.loader.writeLoadTag("script", google.loader.ServiceBase + "/api/search/1.0/8c68537a8c14de310f268bd7f81c9c67/default+en.I.js", false);

which makes a call (where 'n' equals document)

n.write('<script src="' + b + '" type="text/javascript"><\/script>')

Now ideally this js should overwrite the entire page contents with this script. But for some reason it isn't doing so and this is driving me nuts!

Either way, my primary goal is to load this file asynchronously.

Jags
  • 1,466
  • 1
  • 18
  • 33

1 Answers1

0

You should try using jQuery's getScript; it handles all the mess you don't want to be involved in: adding the async attribute, defining both readystatechange and load event handlers, avoiding memory leaks, …

Edit:

As for your root problem: I don't think that you can make this script work if you load it asynchronously. document.write is synchronous by nature. See this related question: Warning: A call to document.write() from an asynchronously-loaded external script was ignored. How is this fixed?

Community
  • 1
  • 1
Julien Royer
  • 1,419
  • 1
  • 14
  • 27
  • I tried that as well. It's still the same. What I can't seem to figure out is why the n.write (document.write) inside the google's code is not working as it should. – Jags Dec 10 '12 at 12:36
  • Yes, sorry, I should have read your question more thoroughly; see my edited answer. – Julien Royer Dec 10 '12 at 12:48
  • ahh..that explains the weird behavior! Thanks Julien for clearing that out – Jags Dec 10 '12 at 13:51