Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable is undefined?

Status
Not open for further replies.

Mike3333

Programmer
Jan 18, 2005
32
US
Hi, i'm getting "Error: 'RotateDelay' is undefinded" with the following script. Can anyone help?

<script language="JavaScript">
<!--

var rotatedelay = 2000;
var currentpic = 0;
var numpics = 6;
picnum = 0;

if(document.images) {

var pics = new Array();

for(i=0;i<numpics;i++) {
pics = new Image();
pics.src="/_images/SustainingMem/rotate" + i + ".png"
}
}

var url = new Array();

url[0] = " url[1] = " url[2] = " url[3] = " url[4] = " url[5] = "

function rotatepic() {

set picnum = Math.Round(Math.random()*(numpics-1));

document.flipper.src=pics[currentpic].src;

setTimeout('rotatepic()',rotatedelay);

}

function gotoURL() {
document.location.href=url[currentpic];
}

// -->

</script>

</HEAD><BODY onLoad="if(document.images) setTimeout('rotatepic()',rotatedelay);" bgcolor="#ffffff" background="/_images/bgnd.png" leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">
 
Well if the error messsage has a capital "R" and "D" in RotateDelay like you say, it means you're calling it incorrectly because the variable name is all lower-case. JavaScript is case sensitive. Also, JavaScript does not use the "set" keyword like vbscript does. Remove it from this:
set picnum = Math.Round(Math.random()*(numpics-1));

Adam
 
Thank you Adam,

It didn't have the caps in the error, that's just me typing that in there. I took out the "SET" like you said, and now I get this error "Error: Object doesn't support this property or method"

Any ideas? I'm trying to randomize pics with a URL link that also changes with the pic.
 
Don't type your code in here, copy and paste it from the actual document. If it's misspelled in the document but you type it in here correctly, no one will be able to find the error.

Lee
 
THANKS FOR YOUR HELP LEE, but I can't copy and paste the error pop-up from internet explorer, but I really do appreciate your snide comment vs. the help!!!!
 
Not a snide comment. A good percentage of the people who post problems here type in something different from what they actually have in the code on their computer. That sends people here chasing things that aren't the problem without letting us know what the real cause is.

I wasn't referring to the popup error, I'm aware that you can't copy and paste code from there.

Javascript is case sensitive, and your

Math.Round()

should be

Math.rround()

Lee
 
Looking over your code again, I'd use

Code:
Math.floor((Math.random()*numpics);

instead of

Code:
Math.round(Math.random()*(numpics-1));

Your results will be slightly different because of rounding up and down.

You don't change the value of currentpic anywhere, and I'd imagine you'd want to use:

Code:
currentpic = Math.floor(Math.random() * numpics);

instead of

Code:
picnum = Math.floor(Math.random() * numpics);

Also, if you rewrite your code a bit, it'll automatically adjust if you add, rearrange, or delete URLs:

Code:
var rotatedelay = 2000;
var currentpic = 0;

var url = new Array(), ui=0;
url[ui++] = "[URL unfurl="true"]http://www.alleneng.com/";[/URL]
url[ui++] = "[URL unfurl="true"]http://www.ashgrove.com";[/URL]
url[ui++] = "[URL unfurl="true"]http://www.ashfordformula.com/";[/URL]
url[ui++] = "[URL unfurl="true"]http://www.bakerconcrete.com/";[/URL]
url[ui++] = "[URL unfurl="true"]http://www.boralmti.com/";[/URL]
url[ui++] = "[URL unfurl="true"]http://www.cmec.org/";[/URL]

var numpics = url.length;

if(document.images)
  {
  var pics = new Array();

  for(i=0;i<numpics;i++)
    {
    pics[i] = new Image();
    pics[i].src="/_images/SustainingMem/rotate" + i + ".png"
    }    
  }

Lee
 
Thank you very much Lee, I really appreciate the valueable help you provide!! The first pic is always the same because of this code. Anything you know of to get it to randomize and to open another window with the target="-blank" tag?

<A HREF="javascript:gotoURL()">
<IMG SRC="/_images/SustainingMem/Rotate0.png" NAME="flipper" BORDER=0 WIDTH=150 HEIGHT=100 VSPACE=0 HSPACE=0>
</A>

 
Instead of:

document.location.href=url[currentpic];

use

window.open(url[currentpic],'newwindow');

This will open a separate window with the selected URL.

Some browsers are picky about this:

document.flipper.src=pics[currentpic].src;

and something more universally functional is:

document.images['flipper'].src=pics[currentpic].src;

Lee
 
...but I can't copy and paste the error pop-up from internet explorer...
Actually, you can if you want. Hightlight the text in the error pop-up and press either <CTRL>-<C> or <CTRL>-<INSERT> - Windows might be an incrediby irritating piece of software, but it can do that much. Typically, your original error crops up due to a VM-implementation flaw in that you haven't cancelled your timer event when your page unloads. The function can get cached successfully, but it references the global variable rotatedelay which will not have been similarly cached.
 
Hey mike333, I see that you are fairly new here. Welcome to TT. Most people who are here helping other are genuinely trying to help. No need to get defensive when they suggest to a newbie that posting their original code would be better. You might notice that you were assisted by mostly people from the MVP list on the right hand side of the page. These people are listed there because many do find them helpful. When you find someone helpful, you can than them by clicking on the link that says "Thank userHandle for this valuable post!"

I hope you enjoy your time here at TT.

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
zen.gif

 
I corrected the double-r typo from my first post in my second one. :)# Shows I should preview before posting, huh? :)#

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top