Skip to content

Instantly share code, notes, and snippets.

@gokaybiz
Last active July 23, 2023 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gokaybiz/c5ed2aabdf1e0a43bf063323ce654042 to your computer and use it in GitHub Desktop.
Save gokaybiz/c5ed2aabdf1e0a43bf063323ce654042 to your computer and use it in GitHub Desktop.
Iframe onclick
/**
* Usage:
* try {
* var Advert = new Advert('adscontainer1', 'https://google.com.tr/');
* Advert.willClick().isOnHover();
* } catch(success) {
* location = success;
* }
*
**/
class Advert {
constructor(id, redirect) {
if (typeof new.target === 'undefined')
throw new TypeError('This is a class; you can\'t call it directly like a function!')
this.clicked = false
this.id = id
this.redirect = redirect
if (sessionStorage.getItem('clicked') === '1')
throw new Error(redirect)
if (!sessionStorage.getItem('clicked'))
sessionStorage.setItem('clicked', '0')
if (!this.getElement(this.id))
throw new Error(redirect)
}
getElement(e) {
return document.getElementById(e)
}
isClicked() {
return this.clicked
}
willClick() {
window.addEventListener('blur', () => {
if (this.clicked) {
sessionStorage.setItem('clicked', '1')
console.log('Wow! You clicked!')
window.setTimeout(() => {
return location = this.redirect
}, 2500)
}
})
return this
}
isOnHover() {
this.getElement(this.id).addEventListener('mouseover', () => {
this.clicked = true
console.log(this.isClicked())
})
this.getElement(this.id).addEventListener('mouseout', () => {
this.clicked = false
console.log(this.isClicked())
})
return this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment