Skip to content

Instantly share code, notes, and snippets.

@datchley
Created October 2, 2013 13:33
Show Gist options
  • Save datchley/6793842 to your computer and use it in GitHub Desktop.
Save datchley/6793842 to your computer and use it in GitHub Desktop.
For each iframe on a page, build an overlay on top of it that captures click events and sends them to the document in the iframe. This is to allow mobile scrolling in webkit, since it seems touching the iframe when swiping won't bubble up the touch event to the main page and hence won't scroll the page. This only works if you control the content…
$(document).ready(function() {
"use strict";
var ident = 0; // generate unique element id
// Find all iframe DFP ads
$('iframe').each(function() {
var width = $(this).width(),
height = $(this).height(),
id = $(this).id, iframeNode = $(this).get(0),
iframeDoc = (iframeNode.contentDocument) ? iframeNode.contentDocument : iframeNode.contentWindow.document,
overlayId = 'iframe-' + (ident) + '-overlay',
iframeId = 'iframe-' + (ident++);
$(this).attr('id', iframeId);
// Prepend an overly div
var overlay = $('<div id="' + overlayId + '">');
overlay
// position it absolutely so it covers the iframe
.css({
'position': 'absolute',
'top': '0px',
'left': '0px',
'opacity': 0,
'width': '100%',
'height': '100%',
'z-index': 400
})
.addClass('clickmask')
// add event handler to dispatch 'click's to the iframe document
.click(function(ev) {
var doc = $(this).next('iframe').contents(), // iframeDoc,
x = ev.offsetX,
y = ev.offsetY,
// link = doc.elementFromPoint(x,y),
clickev = jQuery.Event('click', { 'offsetX': x, 'offsetY': y });
// clickev = doc.createEvent('HTMLEvents');
console.log("iframe doc: ", doc);
console.log("iframe overlay (" + $(this).attr('id') + ") click at position " + x + "," + y);
$('body', doc).trigger(clickev);
// clickev.initEvent('click', true, true);
// link.dispatchEvent(clickev);
});
// put the overlay div before the iframe element
$(this).parent('div').css('position', 'relative'); // because we absolute position overlay
$(this).before(overlay);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment