is there simple expression to access iframe body using jquery.
assume iframe’s id is theframe.
I want to give event handler to iframe.
for example,
$("#theframe").contents().find("body").click(function() {
alert("hello, you touched me~");
});
but, this code doesn’t work in IE.
any alternative idea
help me~
Give a name property for the iframe element. Now you can reference it like this:
Your iframe may not have jQuery so, using standard traversing is a safer way
This will be the body element in the iframe. This is however usable by jQuery on the host page, so:
is your jQuery wrapper for the iframe’s body element.
Be careful to do this only if the iframe is loaded:
UPDATE: tested it in IE, and there is a rather starnge behaviour with it. In firefox you have to wrap it inside a document ready event, but in IE, it has to be outside, right after the iframe element. This version works in IE and Firefox too.
UPDATE 2: a shorter version, but it has its drawback, that you have to specify the source of the iframe in the javascript, but it works in all browsers I tried.
UPDATE 3: And an even simpler way for the end: