[Javascript] detecting a frame with a field

Matt Barton javascript at mattbarton.org
Fri Oct 15 13:31:54 CDT 2004


Yeah - that will happen becuase the fCheckFrame function is being called 
every 100 milliseconds by the window.setInterval() method.

To do the alert just once you could do this (this is the cheap hack method):

// ------
var bDoneAlert = false;
function fCheckFrame() {
   oRightHandDocument = parent.dbconsole.panelwindow.document;
   aElements = oRightHandDocument.getElementsByName ('CO_USERNAME');
   if (aElements.length > 0) {
     if (!bDoneAlert) {
     	alert('hello the form is there now.');
         bDoneAlert = true;
     }
   }
}
window.setInterval (fCheckFrame, 100);
// ------


To do it properly, you use the window.clearInterval() method:

// ------
function fCheckFrame() {
   oRightHandDocument = parent.dbconsole.panelwindow.document;
   aElements = oRightHandDocument.getElementsByName ('CO_USERNAME');
   if (aElements.length > 0) {
     alert('hello the form is there now.');
     window.clearInterval (myTimer);
   }
}
var myTimer = window.setInterval (fCheckFrame, 100);
// ------

Advisory: I haven't tested either of the above, and syntax for the 
clearInterval method is from memory as I'm at home not at work with my 
books.

Hope that helps.

Matt

Dislaimer - My 'c' key is on the fritz at the moment.  Please excuse 
typos etc.

Terry Riegel wrote:
> Matt,
> 
> Thats pretty cool. The documents are running on the same domain, I just 
> have no control over the one. Your script works as fine, but I have a 
> small problem. Here is your script with my modifications.
> 
> Notice, I have it do an alert when the form element shows up. That is 
> working except the alert keeps popping up continually. Is there a way to 
> have it only alert once?
> 
> <script language="JavaScript">
> <!--
> function fCheckFrame() {
> oRightHandDocument = parent.dbconsole.panelwindow.document;
> aElements = oRightHandDocument.getElementsByName ('CO_USERNAME');
> if (aElements.length > 0) {
> alert('hello the form is there now.');
> }
> }
> window.setInterval (fCheckFrame, 100);
> -->
> </script>
> 
> 
> Terry Riegel
> 
> Put Your TimeClock on the Web
> http://www.punchedin.com
> On Oct 15, 2004, at 5:05 AM, Matt Barton wrote:
> 
>     Morning Terry,
> 
>     You'll have difficulties if the documents in the two frames are
>     served from different domains - you'll get "Access Denied" errors in
>     javascript when trying to access the right-hand frame.
> 
>     I know that you haven't said that they are below, but when you said
>     that the right hand frame wasn't under your control, I thought that
>     might have been one of the possibilities.
> 
>     Assuming that the two documents are from the same domain however, I
>     suppose you could use the setInterval () method of the left hand
>     window to call a function every so often which checks the right hand
>     frame. The function could use the getElementsByName method of the
>     document in the right hand window.
> 
>     Something like:
> 
>     // ------------
>     function fCheckFrame () {
>     oRightHandDocument = parent.idOfRightHandFrame.document;
>     aElements = oRightHandDocument.getElementsByName ('CO_USERNAME');
>     if (aElements.length > 0) {
>     document.forms[0].submit ();
>     }
>     }
> 
>     window.setInterval (fCheckFrame, 100);
>     // -------------
> 
>     You would need to know the ID and/or name of the right hand frame,
>     but you'll have control over that won't you? Also you'd need to
>     customise the reference to your form - I just used form[0] to
>     reference the first (only) form in the document in my test case.
>     I've tested this in IE6/Win and Firefox and both are OK.
> 
>     Hope this helps a little. I'd be interested to see if anyones got a
>     better method of triggering the check of the right hand frame, as I
>     don't particularly like window.setInterval - it strikes me as (in
>     this instance) a little inelegant.
> 
>     Ta ta,
> 
>     Matt
> 
>     Terry Riegel wrote:
> 
>         Hello All,
>         I have set up a 2 frame page. The left frame is fully under my
>         control, the right frame is not.
>         +--+------+
>         | | |
>         | | |
>         | | |
>         +--+------+
>         I want the left frame to watch the right frame until it contains
>         a form with a text field named CO_USERNAME
>         The Right frame (I can't control its contents) will look like
>         below.
>         <body>
>         <form method=post action=http://www.page.com>
>         ...
>         <input type=text name=CO_USERNAME size=30 maxlength=30
>         value="Visitor5">
>         ...
>         </form>
>         </body>
>         When the left frame sees a text field labeled CO_USERNAME it
>         will trigger a page load.
>         Can this be done?
>         Terry
>         _______________________________________________
>         Javascript mailing list
>         Javascript at LaTech.edu
>         https://lists.LaTech.edu/mailman/listinfo/javascript
> 
>     _______________________________________________
>     Javascript mailing list
>     Javascript at LaTech.edu
>     https://lists.LaTech.edu/mailman/listinfo/javascript
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript



More information about the Javascript mailing list