views:

38

answers:

2

Given an iFrame with throughout the iframe...

Is it possible to add an event listener with jquery to trigger an ALERT anytime the user moves their mouse over .fineme in the iframe, from the top/parent window?

+1  A: 

See http://api.jquery.com/contents/ for accessing content document of an iframe.

A solution to your question might be:

$("iframe#name").contents().find(".fineme").bind("mouseover", function() { alert("Found me"); });
Sean Hogan
Interesting, but that too doesn't seem to work?
AnApprentice
Works for me. Maybe the class should be `.findme` instead of `.fineme`?
Sean Hogan
+1  A: 

Yea, It seems that by default if you run a selector from the parent window it won't find elements inside the iframe. However, you can explicitly give jQuery the context in order to look inside the iframe.

One way to do this would be:

var iframe = $("#someIframeId");
$(".fineme",iframe.get(0).contentDocument).mouseover(function(){alert('hi')});

NOTE: This will only work if both the parent site and the IFrame are on the same domain. For more information about this see: http://madskristensen.net/post/Iframe-cross-domain-JavaScript-calls.aspx

Matthew Manela
Interesting, but that doesn't seem to work?
AnApprentice
One thing to note: for this to work your iframe must be on the same domain as your page. If they are on different domains I think browser security my stop this from working.
Matthew Manela