/    Sign up×
Community /Pin to ProfileBookmark

undefined variables

Hi,

I have the following code:

[code=php]
<html>

<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″>
<meta name=”GENERATOR” content=”Microsoft FrontPage 4.0″>
<meta name=”ProgId” content=”FrontPage.Editor.Document”>
<title>New Page 2</title>
<base target=”main”>
<script language=’javascript’>
function resize(idpic, srcs, capt){
var fh=parent.main.document.body.clientHeight;
var fw=parent.main.document.body.clientWidth;
var ph=document.getElementById(idpic).height;
var pw=document.getElementById(idpic).width;
var fr=fw/fh;
var pr=pw/ph;
if(fr<pr){parent.main.document.getElementById(“picdis”).innerHTML='<a href=”‘+srcs+'” target=”_blank”><image src=”‘+srcs+'” width=”‘+fw*0.85+'” border=”0″ id=”bigypic”></a>’; }
else{parent.main.document.getElementById(“picdis”).innerHTML='<a href=”‘+srcs+'” target=”_blank”><image src=”‘+srcs+'” height=”‘+fh*0.85+'” border=”0″ id=”bigypic”></a>’; }
parent.capti.document.getElementById(“cap”).innerHTML='<p align=”center”><font size=”2″>’+capt+'</font>’;
winresize(fr, pr, fw, fh);
}
function winresize(fr, pr, fw, fh){
clearTimeout();
var fr=fr;
var pr=pr;
var fw=fw;
var fh=fh;
if(fr<pr){parent.main.document.getElementById(“bigypic”).width=fw*0.85}
else{parent.main.document.getElementById(“bigypic”).height=fh*0.85}
var fr=fr;
var pr=pr;
var fw=fw;
var fh=fh;
setTimeout(“winresize(fr, pr, fw, fh)”,”2000″)
}
</script>
</head>

<body bgcolor=”#000000″ text=”#FFFFFF” vlink=”#0000FF”>
<p><a href=”” onclick=”window.open(‘menu.htm’,”,’toolbar=no,menubar=no,location=no,height=300,width=200,top=100,left=100′); return false”>Menu</a></p>
<p><a href=”javascript:parent.window.close()”>Close</a></p>
<table border=”0″ width=”95″ cellspacing=”0″ cellpadding=”0″ style=”text-align: Center”>
<!–put code here–>
<!–Start picture ID ‘cam’–><tr><td width=”95″ height=”95″><p align=”center”><img border=”0″ src=”chameleon1.bmp” width=”90″ id=”cam” onclick=”resize(‘cam’, ‘chameleon1.bmp’, ‘Chameleon’)”></p></td></tr><!–End picture ID ‘cam’–>

<!–Start picture ID ‘cam2’–><tr><td width=”95″ height=”95″><p align=”center”><img border=”0″ src=”chameleon2.bmp” width=”90″ id=”cam2″ onclick=”resize(‘cam2’, ‘chameleon2.bmp’, ‘Another chameleon’)”></p></td></tr><!–End picture ID ‘cam2’–>
</table>

</body>

</html>
[/code]

The problem is, when [B]setTimeout(“winresize(fr, pr, fw, fh)”,”2000″)[/B] runs winresize() for the second time, an error orrurs which is “fr is undefined”, even though I’ve “defined” all the variables. Does anyone know why this is?

Thanks in advance,

Neil

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisDec 16.2003 — You need to understand variable scope. When you pass something to a function, the name you specify in the function header is a local variable. When you call setTimeout() you are passing a global variable. Therefore, the variable "fr" does not exist when the setTimeout() is called outside of the function.

Besides, you have a lot of things that are incorrect in the function. Try this:
<i>
</i>var timer = null;
function winresize(fr, pr, fw, fh) {
if (timer != null) clearTimeout(timer);
if(fr&lt;pr)
{parent.main.document.getElementById("bigypic").width=fw*0.85}
else
{parent.main.document.getElementById("bigypic").height=fh*0.85}
timer = setTimeout("winresize(" + fr + "," + pr + "," + fw +"," + fh + ")",2000);
}
×

Success!

Help @neil9999 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...