473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

JavaScript across frames - notification when link is clicked

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

Jul 23 '05 #1
6 2806
Tom Braun wrote:
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>

Jul 23 '05 #2
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.

Jul 23 '05 #3
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

Jul 23 '05 #4
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).

Tom Braun wrote:
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


Jul 23 '05 #5
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

Jul 23 '05 #6
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.
Tom Braun wrote:
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


Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve | last post by:
Hi, I have a private website for 20 people that is similar to a web email client like hotmail. There are two frames, one on the left with links for "New", "History", "Todays" and a frame on the...
9
by: Dom | last post by:
I have a frameset with a left right columns, called 'index' and 'content'. The left column of the frameset ('index') loads an index.htm file which have these hrefs (summarized the code a bit)...
8
by: chrisdude911 | last post by:
how do i add video into a javascript web page with my own custom buttons?
3
by: mportman300 | last post by:
Have pity on me.. i have been doing html, css, javascript squarely over 2 years... and am now doing a family project.. in my project i have decided to use a context menu script, its a menu that...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.