Skip to content

Instantly share code, notes, and snippets.

@Ry7n Ry7n/google-ima.js
Created Nov 13, 2012

Embed
What would you like to do?
Loads the google IMA library exactly once.
/*global YUI*/
/**
* Loads the google IMA library exactly once.
*
* https://developers.google.com/interactive-media-ads/docs/sdks/googlehtml5
*
* @module google-ima
* @requires event-custom
*/
YUI.add("google-ima", function (Y) {
var IMA_LOADED = "ima:loaded",
win = Y.config.win,
yLangIsFunction = Y.Lang.isFunction,
loadTO,
onLibLoaded = function (ima) {
Y.Global.fire(IMA_LOADED, ima);
},
onLibSuccess = function () {
onLibLoaded(win.google.ima);
},
onIMALoaded = function () {
clearTimeout(loadTO);
onLibLoaded(win.google.ima);
},
onGoogleLoaded = function () {
var google = win.google;
if (google && google.ima) {
onLibSuccess(google.ima);
} else if (google && google.load) {
// if it takes longer than 10 seconds to load
// cancel and call it an error
loadTO = setTimeout(onLibLoaded, 10 * 1000);
google.load("ima", "1", { callback: onIMALoaded });
} else {
onLibLoaded();
}
};
if (!Y.Global.getEvent(IMA_LOADED, true)) {
Y.Global.publish(IMA_LOADED, { fireOnce: true });
if (win.google) {
onGoogleLoaded();
} else {
Y.Get.script("http://www.google.com/jsapi", {
onSuccess: onGoogleLoaded,
onFailure: onLibLoaded
});
}
}
/**
* loads the IMA library as quickly as possible, while only loading the
* library exactly once.
* @method loadGoogleIMA
* @static
* @for YUI
* @param {Function} cb Called when the library is loaded. Passed the google.ima object as an argument.
* @return {CustomEvent} An event handler for when the library is loaded.
**/
Y.loadGoogleIMA = function (cb) {
return Y.Global.once(IMA_LOADED, function (g) {
if (yLangIsFunction(cb)) {
cb(g);
}
});
};
}, "3.5.0", { requires: ["event-custom"] });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.