...

View Full Version : JavaScript alert command changes results



Alvin
12-03-2008, 03:10 AM
I have created an iFrame:


<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:


<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:


var fnOutput = myFrame.frameFn();


When I examine the contentWindow alone with an alert:


alert('myFrame: '+myFrame);


Everything is well. I either see "myFrame: [object]" (IE) or "myFrame: [object Window]" (Firefox). And my fnOutput variable has valid results:



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.

bassnrage
12-03-2008, 08:47 AM
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! (http://javascriptformvalidator.com)

Alvin
12-03-2008, 02:11 PM
Thanx for your post, mike.


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:



/*
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:



/*
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.


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.


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. :o

Alvin
12-03-2008, 02:25 PM
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. :confused:

Alvin
12-03-2008, 04:19 PM
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:



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? :rolleyes:

vwphillips
12-03-2008, 06:01 PM
first thought


var myFrame = document.getElementById('myFrame').contentWindow;

variable names and ids must be unique

test


<!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>



<!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

Alvin
12-03-2008, 07:29 PM
Thanx for your post and going through all that effort, Vic.



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:

<!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.

Alvin
12-03-2008, 11:14 PM
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:



<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:



<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. :mad:

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.

Alvin
12-03-2008, 11:37 PM
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. :D

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.

vwphillips
12-04-2008, 10:53 AM
<iframe ... onload="setTimeout(function(){ getFrameValue(); },2500);">

bassnrage
12-05-2008, 05:17 AM
yay! glad you got it handled! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum