Skip to content

Instantly share code, notes, and snippets.

@levonlee
Created September 11, 2017 21:29
Show Gist options
  • Save levonlee/1e162c34af377d04364295d027a33c96 to your computer and use it in GitHub Desktop.
Save levonlee/1e162c34af377d04364295d027a33c96 to your computer and use it in GitHub Desktop.
Interstial JW Player on DFP
// Place this as Custom Code on DFP
// https://support.jwplayer.com/customer/portal/articles/1406723
<script src="//content.jwplatform.com/libraries/PLAYERID.js"> </script>
<div style="width:300px;height:250px;" id="InterstitialDiv"></div>
<script type="text/javascript">
(function() {
'use strict';
var temp = '%%VIEW_URL_UNESC%%';
window.onload = function() {
function receiveMessage(e) {
if (e.data == 'load_interstitial_ad') {
var playerInstance = jwplayer("InterstitialDiv");
playerInstance.setup({
file: "//content.jwplatform.com/videos/MEDIAID-TEMPLATEID.mp4"
});
}
if (e.data == 'stop_interstitial_ad') {
jwplayer("InterstitialDiv").remove();
}
}
window.addEventListener('message', receiveMessage);
var sendData = {action:"dfp_load_interstitial_ad", width:"300", height:"250"};
parent.postMessage(sendData,'*');
}
})();
</script>
<script>
var dfpSlots = {}; // non single request mode
googletag.cmd.push(function() {
// non single request mode
dfpSlots.floating = googletag.defineOutOfPageSlot('/123456/floating', 'interstitial').addService(googletag.pubads());
// single request mode
googletag.defineOutOfPageSlot('/123456/floating', 'interstitial').addService(googletag.pubads());
googletag.pubads().addEventListener('slotRenderEnded', function (event) {
if (event.slot.getAdUnitPath() === '/123456/floating') {
document.getElementById('interstitial').style.display = 'none';
}
});
});
</script>
<script>
var dfpInterstitial = {};
dfpInterstitial.loadCount =0;
function ReceiveMessage(e) {
if ( typeof e.data === 'object' && 'action' in e.data && e.data.action == 'dfp_load_interstitial_ad' && dfpInterstitial.loadCount == 0) {
dfpInterstitial.loadCount++;
dfpInterstitial.width =e.data.width;
dfpInterstitial.height =e.data.height;
var cssLink = parent.jQuery("<link rel='stylesheet' href='https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/themes/ui-lightness/jquery-ui.css'>");
jQuery('head').append(cssLink);
[ 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js','/sites/all/themes/abc/js/interstitial.js'
].forEach(function(src) {
var script = document.createElement('script');
script.src = src;
script.async = false;
document.head.appendChild(script);
});
}
}
window.addEventListener('message', ReceiveMessage, false);
</script>
<div id='interstitial'>
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('interstitial'); });
</script>
</div>
// Place this file on server
jQuery(function(){
var targetDialog = '#interstitial';
var iframeId = jQuery(targetDialog).find('iframe').get(0).id;
var adWidth = dfpInterstitial.width;
var adHeight = dfpInterstitial.height;
jQuery(targetDialog).dialog({
width: 'auto',
height: 'auto',
resizable: false,
draggable: false,
closeOnEscape: true,
modal: true,
create: function (event, ui) {
jQuery("#ui-dialog-title-dialog").hide();
jQuery(".ui-widget-content").css({"background-color":"transparent","background":"transparent","border":"none"});
jQuery(".ui-dialog-titlebar").css({"background-color":"transparent","background":"transparent","border":"none"});
jQuery(".ui-dialog-titlebar").removeClass('ui-widget-header');
if (typeof dfpSlots !== "undefined" && typeof dfpSlots.floating !== "undefined") {
googletag.cmd.push(function() {
googletag.pubads().refresh([dfpSlots.floating]);
});
}
jQuery(targetDialog).find('iframe').width(adWidth);
jQuery(targetDialog).find('iframe').height(adHeight);
},
open: function() {
jQuery('.ui-widget-overlay').addClass('custom-overlay');
jQuery(this).closest(".ui-dialog")
.find(".ui-dialog-titlebar-close")
.removeClass("ui-dialog-titlebar-close")
.html("<span class='ui-button-icon-primary ui-icon ui-icon-closethick'></span>");
jQuery(".ui-dialog-titlebar button").css({"border":"none"});
jQuery('.ui-widget-overlay').bind('click',function(){
jQuery(targetDialog).dialog('close');
})
},
close: function() {
jQuery('.ui-widget-overlay').removeClass('custom-overlay');
var receiver = document.getElementById(iframeId).contentWindow;
// Need to stop JW Player
receiver.postMessage('stop_interstitial_ad', '*');
}
});
setTimeout(function () {
jQuery(targetDialog).show();
var receiver = document.getElementById(iframeId).contentWindow;
receiver.postMessage('load_interstitial_ad', '*');
}, 3000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment