Last active
July 23, 2023 17:03
-
-
Save gokaybiz/c5ed2aabdf1e0a43bf063323ce654042 to your computer and use it in GitHub Desktop.
Iframe onclick
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
/** | |
* 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