There are 2 possible solutions:
1) using SafeFrame API
pros:
- u can use it "out of the box"
- you can use it on any website, no custom code is required on the website.
- safe to use
minuses:
- it is limited to filling only the visible area of โโthe website.
- , .
2) API window.postMessage() javascript
:
:
1), SafeFrame API
API ,
GPT Safeframe.
DFP script <head> -
var pageConfig = {
allowOverlayExpansion: true,
allowPushExpansion: true,
sandbox: true
};
googletag.pubads().setSafeFrameConfig(pageConfig);
SafeFrame -. SafeFrame GPT.
SafeFrame -. . "" , <div id="container">, SafeFrame:
<div id="container">
some lines to make container height<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
line<br>
</div>
<script>
var expanded = false;
function expand() {
var self= $sf.ext.geom().self;
var config = {
push: true,
b: 0
};
var el = document.getElementById('container');
var containerHeight = el.offsetHeight;
var expandBottom = containerHeight - self.h;
if(expandBottom < 0) return;
config.b = expandBottom;
$sf.ext.expand(config);
}
function expandDelayed(forceExpand) {
if(expanded && forceExpand || !expanded) {
$sf.ext.collapse();
expanded = false;
setTimeout(expand, 0);
}
}
$sf.ext.register(160, 150, function(status, data) {
if (status === 'geom-update') {
expandDelayed();
}
if (status === 'expanded') {
expanded = true;
}
});
expandDelayed();
</script>
2. API window.postMessage() javascript
DFP script <head> . #hash-tag <iframe> src, .
googletag.pubads().addEventListener('slotRenderEnded', function (event) {
var containerId = event.slot.getSlotElementId();
var containerEl = document.getElementById(containerId);
if (containerEl === null) return;
var iframeEl = containerEl.querySelectorAll('iframe')[0];
var timeoutFunction = function () {
var src = "#" + containerId;
if (iframeEl.getAttribute('src') !== null) {
src = iframeEl.getAttribute('src').replace(/#.*/, "") + src;
} else {
var name = iframeEl.getAttribute('name') + "#" + containerId;
iframeEl.setAttribute('name', name);
}
iframeEl.setAttribute('src', src);
};
setTimeout(timeoutFunction, 10);
});
-, , , .js.
function onMessageReceivedGetStyle(e) {
if (
!(e.origin === 'http://tpc.googlesyndication.com' || e.origin === 'https://tpc.googlesyndication.com') ||
typeof e.data !== 'object' ||
typeof e.data.id !== 'string' ||
e.data.cmd !== 'setStyle' ||
typeof e.data.params !== 'object'
) {
return;
}
var elementId = e.data.id.replace(/#/, "");
var wrapperEl = document.getElementById(elementId);
if (wrapperEl === null) {
return;
}
var elements = [wrapperEl];
if (typeof e.data.query === 'string' && e.data.query) {
elements = wrapperEl.querySelectorAll(e.data.query);
}
elements.forEach(function (element) {
Object.keys(e.data.params).forEach(function (param) {
element.style[param] = e.data.params[param];
});
});
}
if (window.addEventListener) {
addEventListener('message', onMessageReceivedGetStyle, false);
}
else {
if (window.attachEvent) {
attachEvent('onmessage', onMessageReceivedGetStyle);
}
else {
window.onmessage = onMessageReceivedGetStyle;
}
}
-, DFP. ,
, script ,
iframe :
<div id="container">
<a href="#" target="_blank">
<img src="%%FILE:JPG1%%">
</a>
<a href="#" target="_blank">
<img src="%%FILE:JPG2%%">
</a>
</div>
<style>
a {
display: block;
margin-bottom: .5em;
}
img {
display: block;
max-width: 100%;
}
*:last-child {
margin-bottom: 0;
}
</style>
<script>
var container = document.getElementById('container');
function resizeOutsideSafeFrame() {
if (!window.top.postMessage) {
return false;
}
var divGptAdId = '%%PATTERN:url%%';
if (divGptAdId.indexOf('#') !== -1) {
divGptAdId = divGptAdId.split('#')[1];
} else {
divGptAdId = window.location.hash;
}
if(!divGptAdId) {
if (window.name.indexOf('#') !== -1) {
divGptAdId = window.name.split('#')[1];
}
}
if(!divGptAdId) {
return;
}
var width = '100%';
var height = container.offsetHeight + 'px';
window.top.postMessage({
cmd: 'setStyle',
id: divGptAdId,
query: 'div, iframe',
params: {
display: 'block',
height: height,
width: width
}
}, '*');
}
document.onreadystatechange = function () {
if (document.readyState == "complete") {
resizeOutsideSafeFrame();
}
};
setTimeout(resizeOutsideSafeFrame, 100);
</script>
. - - iframe, cmd
- iframe.