I have this code that detects when a click inside the iframe. Within the 'if', if the click, I create a cookie. But the cookie is being created as I enter the page, not only when there is a click.
How can I make the cookie is created only when the click event happen?
focus();
var listener = addEventListener('blur', function() {
if(document.activeElement = document.getElementById('bloco')) {
message.innerHTML = 'Creat a cookie visits';
jQuery.cookie('visits', 1 , { expires: 7, path: '/' });
}else {
message.innerHTML = 'fail';
}
removeEventListener(listener);
});
bloco {
width: 300px;
height: 400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<div id="message"></div>
<div id="bloco">
<iframe src="//example.com" width="300" height="400"></iframe>
</div>