Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Hinsdale, Illinois
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript alert command changes results

    I have created an iFrame:
    Code:
    <iFrame id="myFrame" name="myFrame"...>
    The src file contains a JavaScript function. In the page that contains the iFrame I am trying to have that function return a value:
    Code:
    <script type="text/javascript" language="JavaScript">
    var myFrame = document.getElementById('myFrame').contentWindow;
    The function does not need a parameter, so I want to do this:
    Code:
    var fnOutput = myFrame.frameFn();
    When I examine the contentWindow alone with an alert:
    Code:
    alert('myFrame: '+myFrame);
    Everything is well. I either see "myFrame: [object]" (IE) or "myFrame: [object Window]" (Firefox). And my fnOutput variable has valid results:

    Code:
    alert('fnOutput: '+fnOutput);
    But if I remove the first alert, the Firefox Error Console tells me that myFrame.frameFn is not a function, and the script ends, i.e., I do not get the second alert output.

    Driving me crazy. I hope this is a well known phenomenon, but I've been Googlung this for hours with no results.

  • #2
    New Coder
    Join Date
    Dec 2008
    Location
    Seattle
    Posts
    22
    Thanks
    0
    Thanked 3 Times in 3 Posts
    that's an interesting one... i had something happen like this a few years ago and it was totally bizarre. i ended up leaving the first alert in the page - just commented it out... but it worked. don't know if you are experiencing the same type of weirdness, but it might be worth a try if you haven't tried it yet.

    maybe you could post a more complete view of your code...? did you check your brackets and semi-colons? LOL

    alright, good luck.
    mike
    javascript form validator - FREE!

  • #3
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Hinsdale, Illinois
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx for your post, mike.

    Quote Originally Posted by bassnrage View Post
    i ended up leaving the first alert in the page - just commented it out... but it worked. don't know if you are experiencing the same type of weirdness, but it might be worth a try if you haven't tried it yet.
    I'd inadvertently tried that already, although based on your experience I went back and tried this:

    Code:
    /*
    alert('myFrame: '+myFrame);
    */
    var fnOutput = myFrame.frameFn();
    alert('fnOutput: '+fnOutput);
    ... --more code that does something I can see--
    I'd used the double-slashes comment style before.

    Still failed in Firefox, and the Firefox Error Console still told me that myFrame.frameFn is not a function. But IE (v6.0.2900.2180.more-gobbledygook) displayed the expected value in the uncommented alert. So I continued on:

    Code:
    /*
    alert('myFrame: '+myFrame);
    */
    var fnOutput = myFrame.frameFn();
    /*
    alert('fnOutput: '+fnOutput);
    */
    ... --more code that does something I can see--
    Now IE failed because I did not see the expected result. A moment of hope there.

    Quote Originally Posted by bassnrage View Post
    maybe you could post a more complete view of your code...?
    Sorry, no can do. I've been programming since Hector was a pup, so I think you can trust my nonspecific code.

    Quote Originally Posted by bassnrage View Post
    did you check your brackets and semi-colons? LOL
    Nothing to laugh about, mike--that's an excellent suggestion. Yes, in fact I did find a missing semicolon after the second alert. That got me excited too, but it didn't fix anything when I fixed that.

    But I feel like I'm in a support group because there's someone else who's had the same experience.

  • #4
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Hinsdale, Illinois
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just now went back and removed the code for the second alert to see what IE would do. It failed. Looks like IE needs the second alert, Firefox needs the first one.

  • #5
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Hinsdale, Illinois
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm still trying to answer this question myself, and something I Googled since the last post prompted me to add the following code at the beginning of my script:

    Code:
    var browserName = navigator.appName;
    alert('browserName: '+browserName);
    Then I started playing around with the rest of the code until I finally deleted the other two alerts, and everything works as expected in both IE and Firefox without doing any additional testing. But if I even just comment out the above alert... "Ashes, ashes; we all fall down."

    So, why does the JavaScript alert command cause contentWindow to work as advertised?

  • #6
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,467
    Thanks
    3
    Thanked 495 Times in 482 Posts
    first thought

    Code:
    var myFrame = document.getElementById('myFrame').contentWindow;
    variable names and ids must be unique

    test

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    
    function Test(){
    var myFrame = document.getElementById('fred').contentWindow;
    document.Show.Show0.value=myFrame.Test();
    }
    /*]]>*/
    </script></head>
    
    <body>
    <input type="button" name="" value="Test" onclick="Test();"/>
    
    <iframe name="fred"  id="fred" src="081203S.htm" width="400" height="400"></iframe>
    <script> vic=0; </script>
    <form name=Show id=Show style="position:absolute;visibility:visible;top:420px;left:0px;" >
    <input size=100 name=Show0 >
    <input size=10 name=Show1 >
    <input size=10 name=Show2 >
    <input size=10 name=Show3 >
    <input size=10 name=Show4 >
    <input size=10 name=Show5 >
    <input size=10 name=Show6 >
    <input size=10 name=Show7 >
    <input size=10 name=Show8 >
    <input size=10 name=Show9 ><br>
    <textarea name=TA rows=1 cols=100 ></textarea>
    </form>
    </body>
    
    </html>

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    
    function Test(){
     return 'tom';
    }
    /*]]>*/
    </script></head>
    
    <body>
    081203S
    </body>
    
    </html>
    works with IE and FF
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #7
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Hinsdale, Illinois
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx for your post and going through all that effort, Vic.

    Quote Originally Posted by vwphillips View Post
    variable names and ids must be unique
    Thought I had a new mantra. Certainly need to keep this in mind. But making changes to my variable names didn't help.

    I happen to be using:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    But I'm thinking that shouldn't make a difference because I can get contentWindow to work when I use the alert command.

  • #8
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Hinsdale, Illinois
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I may have found the answer, although Firefox is complaining about it.

    One thing about the JavaScript alert command is that it puts things on hold until one clicks the "OK" button--so I suspected a timing problem. I happened to have found another thread on CodingForums (http://codingforums.com/showthread.php?t=22861) that discussed a problem using the iFrame onload attribute. "Onload?" I said as the light-bulb appeared over my head. So I did this:

    Code:
    <iframe id="myFrame" name="myFrame" ... onload="getFrameValue()">
    Now everything worked in both browsers except that the Firefox Error Console (FEC) was still telling me that myFrame.frameFn is not a function. So I tried to enclose my function in the setTimeout function:

    Code:
    <iframe ... onload="setTimeout(getFrameValue(),2500)">
    But as soon as I introduced setTimeout FEC started complaining "useless setTimeout call (missing quotes around arguments?)" So I put sleeper code inside my getFrameValue function before any other commands were run. It still complained, and now the calling page seemed to take a little longer to display. Arg.

    Seems I'll have to live with the FEC error unless someone has an answer. I'll leave this thread open for another day or two. Thank you.

    And thank you again to mike and Vic for their posts.

  • #9
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Hinsdale, Illinois
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Well, as the old proverb says, "All's well that ends well."

    In my excitement to experiment with the iFrame onload attribute I forgot to remove the script that I inserted below the iFrame to call the getFrameValue function--that was my best guess where it should go. I removed the extra call and not only did everything still work, but the Firefox Error Console stopped generating errors.

    Still gonna keep Vic's advice handy; it might have helped, and certainly it can't hurt. And mike also gets points for playing with some code himself. Thanx, guys.

  • #10
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,467
    Thanks
    3
    Thanked 495 Times in 482 Posts
    Code:
    <iframe ... onload="setTimeout(function(){ getFrameValue(); },2500);">
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #11
    New Coder
    Join Date
    Dec 2008
    Location
    Seattle
    Posts
    22
    Thanks
    0
    Thanked 3 Times in 3 Posts
    yay! glad you got it handled!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •