Forum: Rails Spinoffs (closed, excessive spam) Observe events in iframe

Posted by AlannY (Guest)
on 2008-06-24 14:54
(Received via mailing list)
Is it possible to observe events in iframe?

var iframe = new Element('iframe');

iframe.observe('mouseup', function (e) { ... }); // Not works ;-(

I found that in FF, I can

iframe.contentWindow.document.addEventListener('mouseup', function (e)
{...}, true); // Works

But, how to do it with Prototype?
tnx
Posted by Frederick Polgardy (Guest)
on 2008-06-24 15:00
(Received via mailing list)
Not exactly.  As you can see from your example, the iframe's content is
literally a different document, with its own namespace.  So if Prototype
were loaded in the iframe's document, you could say
iframe.contentWindow.document.observe(...).

-Fred

On Tue, Jun 24, 2008 at 7:53 AM, AlannY <m@alanny.ru> wrote:

> {...}, true); // Works
>
> But, how to do it with Prototype?


--
Science answers questions; philosophy questions answers.
Posted by Luca Guidi (Guest)
on 2008-06-24 15:04
(Received via mailing list)
You should observe the window inside the frame, the code is something
like this:

function contentWindow(frame){
   if(Prototype.Browser.IE)
     return frame.contentWindow.document;
   return frame.contentDocument.defaultView;
}

contentWindow = contentWindow($('my_frame'));
Event.observe(contentWindow, 'click', function(e){/*...*/});

--
blog: www.lucaguidi.com
Pro-Netics: www.pro-netics.com
Sourcesense - making sense of Open Source: www.sourcesense.com
Posted by AlannY (Guest)
on 2008-06-25 17:04
(Received via mailing list)
Firefox says:
contentWindow.observe is not a function ;-) Like before ;-(
Posted by AlannY (Guest)
on 2008-06-25 17:08
(Received via mailing list)
Is Prototype loaded to the iframe automaticaly, or I should load
manually? If second: how to do it?
Posted by Frederick Polgardy (Guest)
on 2008-06-25 17:08
(Received via mailing list)
contentWindow.observe isn't a function because A) it's in the other 
frame
and you haven't loaded Prototype into it, and B) because the window 
object
probably isn't extended anyway.  The code below uses
Event.observe(...contentWindow), not contentWindow.observe().

-Fred

On Wed, Jun 25, 2008 at 10:03 AM, AlannY <m@alanny.ru> wrote:

>
> Firefox says:
> contentWindow.observe is not a function ;-) Like before ;-(


--
Science answers questions; philosophy questions answers.
Posted by Frederick Polgardy (Guest)
on 2008-06-25 17:09
(Received via mailing list)
The body of the iframe document would need to have the <script> tag for
Prototype.

-Fred

On Wed, Jun 25, 2008 at 10:07 AM, AlannY <m@alanny.ru> wrote:

> > -Fred
--
Science answers questions; philosophy questions answers.
Posted by Luca Guidi (Guest)
on 2008-06-25 17:17
(Received via mailing list)
As Frederick said, you cannot invoke #observe on contentWindow, because
it isn't a Prototype extended element.

You should use Event.observe syntax.

Luca
--
blog: www.lucaguidi.com
Pro-Netics: www.pro-netics.com
Sourcesense - making sense of Open Source: www.sourcesense.com
Posted by kangax (Guest)
on 2008-06-25 17:28
(Received via mailing list)
I was playing with #extendIframe method some time ago
http://github.com/kangax/protolicious/tree/master/...
Unfortunately, it only works in FF/Safari (need to find some time to
tackle the problems in IE/Opera)

- kangax
Posted by AlannY (Guest)
on 2008-06-25 20:16
(Received via mailing list)
Thank you all ;-)
Posted by Matt Foster (Guest)
on 2008-06-25 21:16
(Received via mailing list)
Be cautious of listening to the iframe's document, if that iframe
refreshes, its a brand new document, and in that case your listener is
lost, so you'd have to listen to the load event and grab a fresh
reference to stay persistent.

--
Matt Foster
Ajax Engineer
Nth Penguin, LLC
http://www.nthpenguin.com
Posted by AlannY (Guest)
on 2008-06-26 17:54
(Received via mailing list)
Still not works ;-(

var iframe_doc;
if( Prototype.Browser.IE ) {
    iframe_doc = iframe.contentWindow.document;
} else {
    iframe_doc = iframe.contentDocument.defaultView;
}

Event.observe(iframe_doc, 'click', function (e) { alert("click"); });
Posted by AlannY (Guest)
on 2008-06-26 18:09
(Received via mailing list)
I have search on the NET and seems that there are exists some bugs in
FF with iframs's onfocus event, which I need ;-) But it not works even
in IE ;-(

I'm not loading the page of iframe from the outside, so, maybe, it's
better to load Prototype in the iframe?
This topic is locked and can not be replied to.