iframe and attachevent

F

foldface

Hi
Completely ignore the non-standard nature of the noddy example
below.
I want to capture a mouse click in a frame from outside that frame.
Everything
I read online seems to indicate its something like the below but I
just can't get it to work. any ideas?
Ta
F

<script>
function Temp()
{
alert('hi')
}

function SetUp()
{
var myIFrame2 = document.getElementById("frame2");

myIFrame2.attachEvent('onclick', Temp);
}
</script>

<body onload=SetUp()>
<form>
<IFRAME id=frame2 src="http://www.google.co.uk" width=100%
height="700" scrolling="auto">
[Your user agent does not support frames or is
currently configured
not to display frames. However, you may visit
<A href="temp2.html">the related document.</A>]
</IFRAME>
</form>
</body>
 
R

Richard Cornford

Completely ignore the non-standard nature of the
noddy example below.

So long as the example actually relates to the problem.
I want to capture a mouse click in a frame from
outside that frame.

For which you would have to attach an event listener to the document
object within the IFRAME.
Everything I read online seems to indicate its something
like the below but I just can't get it to work. any ideas?

Find alternative reading matter.
<script>
function Temp()
{
alert('hi')
}

function SetUp()
{
var myIFrame2 = document.getElementById("frame2");

myIFrame2.attachEvent('onclick', Temp);

There is a distinction between the IFRAME element in the containing
document and the frame object that contains the IFRAME's content. You
are attaching your listener to the IFRAME element. If it had borders you
probably could pick up clicks on those borders, but interaction with the
contents should not be expected to propagate to the containing document
in any way.
}
</script>

<body onload=SetUp()>
<form>
<IFRAME id=frame2 src="http://www.google.co.uk" width=100%
<snip> ^^^^^^^^^^^^^^^^^^^^^^^

Unless you are working for google uk this is exactly the type of
situation that cross-domain/same-origin security is intended to prevent.
Monitoring user interaction with a page from a different domain, if
successful, would be open to considerable abuse. Consider monitoring
someone's interaction with the log-in page at their bank's web site, for
example (if onclick then why not onkeydown, and read their user name and
password as they type it in?).

You shouldn't expect this desire to be achievable (and your on-line
reading should have warned you of the issue).

Richard.
 
F

foldface

Richard Cornford said:
So long as the example actually relates to the problem.

I've had people commenting that they won't help unless the example is
w3
compliant and multi-browser, bit silly for a "noddy example"
Find alternative reading matter.

open to suggestions here. Problem is I only work on IE as its the only
browser
we support, and naturally it doesn't support everything, has bugs,
etc. Also
most texts I find don't seem to go into this kind of detail, and
javascript is
very forgiving which makes things harder for me. Perl was the same
until I learnt about 'use strict' to make it catch more bugs.
You shouldn't expect this desire to be achievable (and your on-line
reading should have warned you of the issue).

it did, noddy example again. The real example is all local and
accessed (for now) via http:/localhost/Temp/temp.html, hence fine
cross scripting wise. I'm sure that I've seen error messages from the
browser previously though about this or I would have remembered.

I've got this working now with something like the below

myIFrame1.contentWindow.document.attachEvent('onmousedown',
function(){FireFunc('frame1');})

I don't get an 'event' object though? How do you access the x,y
position of the mouse?

Thanks for your help
F
 
R

Richard Cornford

I've had people commenting that they won't help unless the
example is w3 compliant and multi-browser, bit silly for
a "noddy example"
<snip>

I can't say I have seen many people complaining that they won't help
with IE only stuff. Unless you mean Thomas Lahn, but he is not very
rational so you can safely disregard his opinion. It is true that IE
only stuff gets less attention, but that is mostly because writing for
one known environment is so trivial compared to cross-browser work that
it isn't very interesting (so of little entertainment value).

However, you could try posting to microsoft.public.scripting.jscript,
where IE only questions are the natural subject of the group.
it did, noddy example again. The real example is all local
and accessed (for now) via http:/localhost/Temp/temp.html,
hence fine cross scripting wise. ...
<snip>

That is exactly the sort of detail that needs to be presented up front
(it would, after all, have saved me spending any time on the subject at
all).
I've got this working now with something like the below

myIFrame1.contentWindow.document.attachEvent('onmousedown',
function(){FireFunc('frame1');})

Are you looking in the right place for the event object? The event will
belong to the IFRAME's global/window object not the containing page's.
But the attached function, and the function it is calling, do belong to
the containing page so unqualified - event - and - window.event - both
refer to properties of its global/window object. If you want to write IE
only code then:-

myIFrame1.contentWindow.event

- should refer to the event object in that iframe. (Though I would
recommend accessing IFAMEs and frames through the - frames - collection
as the results are cross-browser (obviously works with IE as well); it
will require less reworking when IE's market share drops to the point
where even the most intransigent commercial sites realise they need to
accommodate other browsers.)

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top