Skip to content

Instantly share code, notes, and snippets.

@KeithNdhlovu
Created July 10, 2020 11:08
Show Gist options
  • Save KeithNdhlovu/1d1abd0ea596205ae772836d5b618539 to your computer and use it in GitHub Desktop.
Save KeithNdhlovu/1d1abd0ea596205ae772836d5b618539 to your computer and use it in GitHub Desktop.
// Instance of Ad
var banner = new Ad(params)
// Set the name of the hidden property and the change event for visibility
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") { // Opera 12.10 and Firefox 18 and later support
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
// If the page is hidden, pause tracking impressions
// if the page is shown, resume tracking impressions
function handleVisibilityChange() {
if (document[hidden]) {
banner.pause()
} else {
banner.resume()
}
}
// Warn if the browser doesn't support addEventListener or the Page Visibility API
if (typeof document.addEventListener === "undefined" || hidden === undefined) {
console.log("This SDK requires a browser, such as Google Chrome or Firefox, that supports the Page Visibility API.");
} else {
// Handle page visibility change
document.addEventListener(visibilityChange, handleVisibilityChange, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment