How to release function only when left-clicking div

Asked

Viewed 54 times

1

Currently I have this code, it checks if you had the click inside the DIV and if you have it will activate the function "okay". The problem is that this function is activated when the right or left click occurs, and I would like to activate it only when the left click, and if the direct click, display a message.

$(document).ready(function() {
        $(".iframe").iframeTracker({
            blurCallback: function() {
                console.log("blurCallback!");
                    okay(); 
            }
        }); 
});

If there is any alternative to this php function, I have accepted.

1 answer

1


It is impossible to do this because the click event is never triggered in an iframe. It can be "simulated" this way:

  1. He identifies that there is a focus.
  2. When you click the focus is lost and as the mouse was on top of the iframe it does some action.

That’s why it’s blurCallback and not clickCallback for example. Therefore, whatever button you used, it just identifies the loss of focus. But you don’t know if it came from a mouse click for example.

Take a test

Place mouse on top of iframe and press tab for you to see what happens.

A "solution" (it is said that)

Is to put a div transparent on top of it (iframe) to "cheat" the user, as if clicking on the iframe. Take an example:

jQuery(document).ready(function(){
	$(".iframe").mousedown(function(e){
		if(e.button == 2) { 
			alert("Clicou com o direito");
		} else {
			alert("Clicou com o esquerdo");
		}
	}); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="iframe" style="position: absolute;width: 300px; height: 300px;">
	MEU IFRAME DE MENTIRA
</div>
<iframe style="width: 300px; height: 300px;" src="https://www.w3schools.com"></iframe>

Illustration of the solution:

inserir a descrição da imagem aqui

  • 1

    Thank you so much for the explanation, man

  • @Rafaelmacedo tranquil mano! = ) I’m happy to help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.