Skip to content

Instantly share code, notes, and snippets.

@phuesler
Last active December 11, 2015 21:48
Show Gist options
  • Save phuesler/4664510 to your computer and use it in GitHub Desktop.
Save phuesler/4664510 to your computer and use it in GitHub Desktop.
modified epom show_ads.js
/*
* Asynchronous loading of ads.
* Below is of the top of my head rewrite to load multiple ads at the same time
* in a asynchronous fashion.
*
* I gave it a quick test run and it seems to work as fare as I can see
*
*
* var EpomConfig = EpomConfig || {ads : []};
* (function(){
* EpomConfig.ads.push(
* {
* key: "some key",
* codeFormat : "ads",
* targetId : "topBanner",
* click : "",
* customParams : {},
* height : 250,
* width : 760
* },
* {
* key: "some key",
* codeFormat : "ads",
* targetId : "topBanner",
* click : "",
* customParams : {},
* height : 250,
* width : 760
* });
* var epom = document.createElement('script');
* epom.type = 'text/javascript';
* epom.async = true;
* epom.src = (location.protocol == 'https:' ? 'https:' : 'http:') + '//www.adshost2.com/js/show_ads.js';
* var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(epom, s);
* })();
*
*/
var EpomAdServer = EpomAdServer || {};
EpomAdServer.addCampaignId = function (campaignId) {
if (typeof this.campaignIds === "undefined") {
this.campaignIds = campaignId;
} else {
this.campaignIds += "," + campaignId;
}
};
EpomAdServer.getCampaignIds = function () {
return this.campaignIds || "";
};
EpomAdServer.getEpomCookies = function () {
if (typeof localStorage !== "undefined" && typeof localStorage.epomCookies !== "undefined") {
var result = [];
var params = JSON.parse(localStorage.epomCookies);
for (var p in params)
result.push(p + "=" + params[p]);
return result.join("&");
}
return "";
};
var EpomInvokeServer = function EpomInvokeServer(config){
this.key = config.key;
this.params = config.params;
this.width = config.width;
this.height = config.height;
this.type = config.codeFormat;
this.baseUrl = (location.protocol == 'https:' ? 'https:' : 'http:') + '//www.adshost2.com';
this.targetId = config.targetId;
if (typeof(config.click) === 'undefined') {
this.click = "";
} else {
this.click = config.click;
}
if (typeof(config.params) === 'undefined') {
this.params = {};
} else {
this.params = config.params;
}
if (typeof(config.width) !== 'undefined') {
this.width = config.width;
}
if (typeof(config.height) !== 'undefined') {
this.height = config.height;
}
}
EpomInvokeServer.prototype = {
toQueryString : function(params){
var result = [];
for (var p in params)
result.push(p + "=" + params[p]);
return result.join("&");
},
getUrl : function(){
var els = document.getElementsByTagName('script');
var el = els[els.length - 1];
var src = el.src;
return this.baseUrl + "/" + this.type + "?v=1&key=" + this.key +
"&click=" + this.click + "&tz=" + (new Date().getTimezoneOffset() / -60) + "&t=" + new Date().getTime() +
"&" + this.toQueryString(this.params) + '&' + EpomAdServer.getEpomCookies();
},
invokeIframe : function () {
var iFrame = document.createElement('iframe');
iFrame.src = this.getUrl();
iFrame.width = this.width.toString();
iFrame.height = this.height.toString();
iFrame.frameBorder = "0";
iFrame.scrolling = "no";
document.getElementById(this.targetId).appendChild(iFrame);
},
invokeJsSyncCode : function () {
var e = document.createElement('script');
e.type = 'text/javascript';
e.async = true;
e.src = this.getUrl() + "&cIds=" + EpomAdServer.getCampaignIds();
document.getElementsByTagName('body')[0].appendChild(e);
},
invoke: function()
{
if(this.type == "ads")
{
this.invokeIframe();
}
else
{
this.invokeJsSyncCode();
}
}
}
var EpomConfig = EpomConfig || {ads:[]};
var ads = EpomConfig.ads;
for (var i = 0; i < ads.length; i++) {
new EpomInvokeServer(ads[i]).invoke();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment