Don't work jQuery click() method.?

I get a script and it have image and link, i want after click on image alert ok and after alert go to a link (href). That’s a cross domain iframe.How is it?

HTML:

<div class="addv">
    <script type="text/javascript">var anetwork_pram = anetwork_pram || [];anetwork_pram["aduser"] = "1423687058";anetwork_pram["adheight"] = "250";anetwork_pram["adwidth"] = "300";</script><script type="text/javascript" src="http://static-cdn.anetwork.ir/showad/pub.js"></script>

</div>

Js Code:

$(".addv").on("click", "a", function(event) {
    alert('ok'); //should alert ok
});

DEMO: http://jsfiddle.net/x5ruvg2n/1/

What do i do?

Having the ad there doesn’t do the alert. Without it, it clicks though.

I want click on iframe and get alert.?

You also took out the “a”.

Maybe if you changed the “a” to “iframe”?
Of course there would need to be one inside the addv div for either to work.

True. Although I ddi test and changed it to div/iframe/etc, and it didn’t work.

I was googling and apparently it won’t work without revamping it. He meant ot target the anchor inside of the iframe. Perhaps he needs to put this jquery inside of the iframe page.

There is no click event for an iframe and you would probably need something like this to do it properly.

You could however absolutely place a link over the top of the iframe and set the destination of the link to the same as that you wanted for the iframe. You could then add an alert to the link.

e.g.

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
.addv {
	position:relative;
	float:left;
}
.mylink {
	position:absolute;
	top:0;
	left:0;
	width:100%;
	height:100%;
	z-index:2;
}
</style>
</head>

<body>
<div class="addv"> <a href="http://www.google.com" class="mylink"></a> 
		<script type="text/javascript">var anetwork_pram = anetwork_pram || [];anetwork_pram["aduser"] = "1423687058";anetwork_pram["adheight"] = "250";anetwork_pram["adwidth"] = "300";</script><script type="text/javascript" src="http://static-cdn.anetwork.ir/showad/pub.js"></script> 
</div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script>

    $("body").on("click", ".mylink", function(event) {
        alert('ok'); //should alert ok
    });
</script>
</body>
</html>

Of course that means the iframe is basically no more than image and will have no interaction.

ok.Very true, How is after alert ok it go to image url (a:link html tag in iframe)?

I’m not quite sure of the question but if you are referring to my demo then the alert is triggered on the anchor.

<a href="http://www.google.com" class="mylink"></a> 

After you close the alert the anchor just goes on about its usual business and follows the clicked link via its href attribute - as usual.

You can’t have any link in the iframe (or the iframe itself) be part of an onclick routine (as mentioned in that link I already gave above).

I assumed you just wanted to click the iframe and go to a pre-destined destination which the code I supplied will do. :smile:

I tried as, see it: http://jsfiddle.net/jwwppgvr/1/ for geting image link and redirection to it. but don’t work for me. ?

$(function(){
    $(".addv iframe").load(function(){
         var iBody = $(".addv iframe").contents().find("body");
         var iDiv = iBody.find("a");
        alert(iDiv.html())
         /*code to change the div content*/
    });    
});

Normally, when you click on the image opens a new page.Now I want to give a alert before opening a new tab.(I want to open in a new page will show the alert .) How can it be done?

You can’t find anything in an iframe. Its not yours to find. It’s another site.

Think of an iframe as another site. You can’t put code on your page and have it affect someone else’s site.

The only way to can have control over an iframe is if you have access to the iframe’s code and then you can add code inside the iframe that will allow you to communicate with the parent page. (Or if the iframe is on the same domain then some browsers allow limited access.)

You can only do what I have shown and send the user to a link that you know. You can’t send them to a random link in an iframe.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.