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!

Coordinated output...

Status
Not open for further replies.

tryangle

Technical User
Mar 19, 2001
49
US
I'm using Math.random() to generate a random picture in a table, and it works fine. But then I'd like to put another graphic- in a different table that coordinates with the first one... I did a document.write(string) of all the code in-between set to a variable- including the sets of graphics, for each of them- and that works too, except I've got this in an external .js file that I'm using for numerous pages and there is page-specific content in-between the graphics that I really can't do without, or generalize. Soooo... Is there some way I could coordinate the output of a Math.random() object/function on both sides of the content in the middle? Thanks~ %-) OK, Who stopped payment on my reality check?
 
You could create a flag variable, and then check to see if it exists. If it doesn't then execute the first set of code and set the flag variable, if it does exist then goe to the 2nd set.

// external.js file
if(!window.vFlag){
// Random function and top image(s) go here
document.write("Top Image");

// Set flag so the next time this script is called in the same page, it goes else
vFlag = 1;
}else{
// Bottom image(s) go here
document.write("Bottom Img");
}

Then in your page, link the js file twice.

<!-- Call the top code -->
<SCRIPT LANGUAGE=&quot;JavaScript&quot; SRC=&quot;external.js&quot;></SCRIPT>

My Middle HTML Goes here

[COLOR=666666]<!-- Call the bottom code -->[/color]
<SCRIPT LANGUAGE=&quot;JavaScript&quot; SRC=&quot;external.js&quot;></SCRIPT> - tleish
 
Thanks tleish, I sure appreciate your help! The //coments are great! It looks like this'll work... I think I just need to mull it over and play with it a little bit to understand it better. I guess I'm not clear about what !window means. Not/opposite window? Is that there because it's in an external file, or would it be there anyway? %-) OK, Who stopped payment on my reality check?
 
You can use window.variable to check to see if a variable exists or if it's been defined yet.

If tried it one of these ways it would throw an error:

if(!vFlag) or if(vFlag == null)

if(!window.vFlag) won't throw an error if the variable hasn't been defined yet.
- tleish
 
Ahh! I see... thanks tleish! OK, Who stopped payment on my reality check?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top