JavaScript across frames - notification when link is clicked

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Braun

    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.onClic k = 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

  • Csaba Gabor

    #2
    Re: JavaScript across frames - notification when link is clicked

    Tom Braun wrote:[color=blue]
    > I need to monitor if someone clicked on some (any) link in a[/color]
    subframe.[color=blue]
    > Due to certain restrictions, the only place I can put some JavaScript
    > is in the main (frameset) page, though.[/color]

    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;noth ing.htm&quot; id=foo
    onClick=&quot;p arent.clickFunc (this);return false;&quot;>
    <u>Try me</u></a></body></html>'">
    </frame>
    </frameset>
    </body>
    </html>

    Comment

    • Random

      #3
      Re: JavaScript across frames - notification when link is clicked

      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.onCli ck = 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.onCli ck = function () {
      // figure out if it's a link and do stuff
      }

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

      Comment

      • Tom Braun

        #4
        Re: JavaScript across frames - notification when link is clicked

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

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

        </frameset>
        </html>
        ---

        When I do that I get an error: window.frames.F S_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

        Comment

        • Random

          #5
          Re: JavaScript across frames - notification when link is clicked

          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:[color=blue]
          > Hello!
          > Thank you for the response. I experimented around with your
          > suggestion, but ran into a problem. First I tried this:
          >
          > ---
          > <html><head><ti tle>Framset</title></head>
          > <script type="text/javascript" language="JavaS cript">
          > window.frames['FS_1'].document.onCli ck = function () { alert('Test
          > message!'); };
          > </script>
          > <frameset cols="200,600">
          > <frame name="FS_1" src="fs1.html" noresize frameborder="ye s"
          > scrolling="no">
          > <frame name="FS_2" src="fs2.html" noresize frameborder="ye s"
          > scrolling="auto ">
          >
          > </frameset>
          > </html>
          > ---
          >
          > When I do that I get an error: window.frames.F S_1 has no properties.[/color]
          I[color=blue]
          > assume this happens, because the frame is not defined at that point
          > yet? When I place the JavaScript AFTER the framset definition, then[/color]
          it[color=blue]
          > does not get executed at all.
          >
          > Where do I need to place JavaScript code, which refers to subframes?
          >
          > Thank you very much...
          >
          > Tom[/color]

          Comment

          • Tom Braun

            #6
            Re: JavaScript across frames - notification when link is clicked

            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><ti tle>Framset</title>
            <script type="text/javascript" language="JavaS cript">
            <!--
            function blah() {
            window.frames['FS_1'].document.oncli ck = function () {
            alert('Test message!'); }
            }
            //-->
            </script>
            </head>
            <frameset cols="200,600" onload="blah(); ">
            <frame name="FS_1" src="fs1.html" noresize frameborder="ye s"
            scrolling="no">
            <frame name="FS_2" src="fs2.html" noresize frameborder="ye s"
            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

            Comment

            • Random

              #7
              Re: JavaScript across frames - notification when link is clicked

              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:[color=blue]
              > 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[/color]
              this[color=blue]
              > and just figuring things out as I go):
              >
              > ---
              > <html><head><ti tle>Framset</title>
              > <script type="text/javascript" language="JavaS cript">
              > <!--
              > function blah() {
              > window.frames['FS_1'].document.oncli ck = function () {
              > alert('Test message!'); }
              > }
              > //-->
              > </script>
              > </head>
              > <frameset cols="200,600" onload="blah(); ">
              > <frame name="FS_1" src="fs1.html" noresize frameborder="ye s"
              > scrolling="no">
              > <frame name="FS_2" src="fs2.html" noresize frameborder="ye s"
              > scrolling="auto ">
              > </frameset>
              > </html>
              > ---
              >
              > That way, the function is defined earlier, but not executed yet.[/color]
              Only[color=blue]
              > 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[/color]
              down[color=blue]
              > below the framset does not help. In fact, I did a very simple
              > JavaScript, which just opened an alert() window. If the code was[/color]
              below[color=blue]
              > the frameset, it simply was not called. This was in Firefox. Does[/color]
              the[color=blue]
              > same happen in IE or other browsers? Strange.
              >
              > Thank you very much for your help...
              >
              > Tom[/color]

              Comment

              Working...