JavaScript across frames - notification when link is clicked

T

Tom Braun

Hello!

My first post here...

I need to monitor if someone clicked on some (any) link in a subframe.
Due to certain restrictions, the only place I can put some JavaScript
is in the main (frameset) page, though.

It appears you can do somehing like document.onClick = blah to ensure
that the function blah is called when someone clicks on a link in the
current document. But what do I need to do when I would like blah to be
called when a link in a subframe of the current document (frameset) was
clicked on?

Any help is greatly appreciated.

Thank you very much!

Tom
 
C

Csaba Gabor

Tom said:
I need to monitor if someone clicked on some (any) link in a subframe.
Due to certain restrictions, the only place I can put some JavaScript
is in the main (frameset) page, though.

Your exact meaning was unclear to me, but perhaps you are looking
for something like the below example (the important part is that
onClick="parent.clickFunc(this);return false;").

That having been said, I am curious about one thing in the example
below. In my FF 1.1, the link is not underlined if I omit the
<u> ... </u>. Have I missed something?

Csaba Gabor from Vienna

<html>
<head>
<title>Frame clicks</title>
<script type='text/javascript'>
function clickFunc(oLink) {
alert(oLink.id + " has been clicked!"); }
</script>
</head>
<frameset>
<frame src="javascript:'<html><head><title>Inner</title>
</head><body>
<a href=&quot;nothing.htm&quot; id=foo
onClick=&quot;parent.clickFunc(this);return false;&quot;>
<u>Try me</u></a></body></html>'">
</frame>
</frameset>
</body>
</html>
 
R

Random

Tom, I think I see what you're getting at, but just to clarify: the
pages in the frame, are they on the same domain name as the parent
document (frameset)? If not, it is not feasible to monitor anything
about them. Documents from two different domains (foo.com, bar.com) are
kept strictly separate by the browser.

If they are not, then I have to wonder: why can't you put your JS in
them?

Regardless:
The best way is to attach an onClick event to the frame's document (
for instance:

window.frames[ myFrameIndex ].document.onClick = function () {
// figure out if it's a link and do stuff
}

; or using attachEvent )

Within the function, you will need to determine if the element clicked
was a link (this function will be called if they click ANYTHING in the
frame), for instance using its nodeName or nodeType, then do whatever
you need to do if it's a link.

Now: Assuming you allow the click to succeed... you then have to
reattach your function_ref event handler to the onClick event on the
document they click to, but you have to remember that the document will
not load immediately.

I recommend using a setInterval or setTimeout loop to check the
document's status.

Also, if for any reason the target document fails to load, your whole
deal could break altogether, so some exception handling is recommended.

Basically... the fundaments of what you're trying to do are pretty
simple:
window.frames[ myFrameIndex ].document.onClick = function () {
// figure out if it's a link and do stuff
}

But to really make it work right is not so easy.
 
T

Tom Braun

Hello!
Thank you for the response. I experimented around with your
suggestion, but ran into a problem. First I tried this:

---
<html><head><title>Framset</title></head>
<script type="text/javascript" language="JavaScript">
window.frames['FS_1'].document.onClick = function () { alert('Test
message!'); };
</script>
<frameset cols="200,600">
<frame name="FS_1" src="fs1.html" noresize frameborder="yes"
scrolling="no">
<frame name="FS_2" src="fs2.html" noresize frameborder="yes"
scrolling="auto">

</frameset>
</html>
---

When I do that I get an error: window.frames.FS_1 has no properties. I
assume this happens, because the frame is not defined at that point
yet? When I place the JavaScript AFTER the framset definition, then it
does not get executed at all.

Where do I need to place JavaScript code, which refers to subframes?

Thank you very much...

Tom
 
R

Random

Pretty sure you're right on that one, Tom. Try moving the <script> to
the bottom, after the </frameset>, before the </html>.

Also, if I remember correctly, I believe the '.onClick' should be
'.onclick' (this was my mistake, sorry about that).
 
T

Tom Braun

Hello!
It seems I need to make sure I execute the JavaScript only AFTER the
frame is loaded. That can be done like this (I'm all very new to this
and just figuring things out as I go):

---
<html><head><title>Framset</title>
<script type="text/javascript" language="JavaScript">
<!--
function blah() {
window.frames['FS_1'].document.onclick = function () {
alert('Test message!'); }
}
//-->
</script>
</head>
<frameset cols="200,600" onload="blah();">
<frame name="FS_1" src="fs1.html" noresize frameborder="yes"
scrolling="no">
<frame name="FS_2" src="fs2.html" noresize frameborder="yes"
scrolling="auto">
</frameset>
</html>
---

That way, the function is defined earlier, but not executed yet. Only
once the frameset is loaded (and thus FS_1 is known) is the function
called, which refers to FS_1.

Weird, but I guess that's how it goes. Just moving the JavaScript down
below the framset does not help. In fact, I did a very simple
JavaScript, which just opened an alert() window. If the code was below
the frameset, it simply was not called. This was in Firefox. Does the
same happen in IE or other browsers? Strange.

Thank you very much for your help...

Tom
 
R

Random

I don't really have any more input for you here, other than to say that
I never had any trouble with FF or IE executing scripts before or after
the <frameset />. I made one site that relied very heavily on scripts
defined after the </frameset>.

Glad you got it working, though.

Cheers.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top