Last active
December 16, 2015 09:49
-
-
Save MatthewCallis/5415863 to your computer and use it in GitHub Desktop.
Am I in an iFrame? Am I Visible?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _gaq = _gaq || []; | |
(function(win, depth){ | |
var iframe = false; | |
if(win){ | |
if( | |
win.location !== win.parent.location || | |
win.self !== win.top || | |
(win.self.frameElement && (win.self.frameElement+"").indexOf("HTMLIFrameElement") > -1) | |
){ | |
depth = 1 + depth; | |
iframe = true; | |
} | |
} | |
if(iframe === true){ | |
arguments.callee(win.parent, depth); | |
} | |
else if(depth > 1){ | |
console.log('I am in an iframe, ' + depth + ' frames deep.'); | |
// _gaq.push(['_trackEvent', 'iFramed', depth, 'Domain']); | |
return; | |
} | |
})(window, 1); | |
function iframe_depth(win, depth){ | |
var iframe = false; | |
if(win){ | |
if( | |
win.location !== win.parent.location || | |
win.self !== win.top || | |
(win.self.frameElement && (win.self.frameElement+"").indexOf("HTMLIFrameElement") > -1) | |
){ | |
depth = 1 + depth; | |
iframe = true; | |
} | |
} | |
if(iframe === true){ | |
iframe_depth(win.parent, depth); | |
} | |
else if(depth > 1){ | |
console.log('I am in an iframe, ' + depth + ' frames deep.'); | |
// _gaq.push(['_trackEvent', 'iFramed', depth, 'Domain']); | |
return; | |
} | |
} | |
iframe_depth(window, 1); | |
(function(el){ | |
if(!el){ | |
return; | |
} | |
var visible = function(element){ | |
if(element.offsetWidth === 0 || element.offsetHeight === 0){ | |
return false; | |
} | |
var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight, | |
rects = element.getClientRects(), | |
on_top = function(r){ | |
for(var x = Math.floor(r.left), x_max = Math.ceil(r.right); x <= x_max; x++){ | |
for(var y = Math.floor(r.top), y_max = Math.ceil(r.bottom); y <= y_max; y++){ | |
if(document.elementFromPoint(x, y) === element){ | |
return true; | |
} | |
} | |
} | |
return false; | |
}; | |
for(var i = 0, l = rects.length; i < l; i++){ | |
var r = rects[i]; | |
var in_viewport = r.top > 0 ? r.top <= height : (r.bottom > 0 && r.bottom <= height); | |
if(in_viewport && on_top(r)){ | |
return true; | |
} | |
} | |
return false; | |
}; | |
if(visible(el) === true){ | |
_gaq.push(['_trackEvent', 'Visible', 'true', 'Domain']); | |
} | |
})(document.getElementById('leaderboard-container')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment