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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with javascript code

Status
Not open for further replies.

kebab

Technical User
Feb 18, 2001
3
0
0
US
The following is part of a javascript that, depending on the date, directs the program to go outside and read a .js file. The problem is one by one the external .js files are read from line to line. Each external .js file is quite long. I want to save bandwidth and exit out of the javascript once the correct line is executed. Or is there a way the external .js file is not read if the if statement is not satisfied?

if (month == 1 && day <15) {document.write("<script src='h1a.js'><\/script>")}
if (month == 1 && day >15) {document.write("<script src='h1b.js'><\/script>")}
if (month == 2 && day <15) {document.write("<script src='h2a.js'><\/script>")}
if (month == 2 && day >15) {document.write("<script src='h2b.js'><\/script>")}
if (month == 3 && day <15) {document.write("<script src='h3a.js'><\/script>")}
 
First of all, if you have a number of conditions and you only want one of them to be true, don't use "if" for every one of them... do "if, else if, else if, else if, else"...

Code:
if (month == 1 && day <15) {document.write("<script src='h1a.js'><\/script>")}  
else if (month == 1 && day >15) {document.write("<script src='h1b.js'><\/script>")}  
else if (month == 2 && day <15) {document.write("<script src='h2a.js'><\/script>")}  
else if (month == 2 && day >15) {document.write("<script src='h2b.js'><\/script>")}  
else if (month == 3 && day <15) {document.write("<script src='h3a.js'><\/script>")}
else { window.alert("no conditions were true") }

So first it would check if month==1 and day<15, if that's not true then it tries month==1 and day>15... if that one is true, then it doesn't even continue checking the other conditions. if it's not true, it continues down the list and checks the next one, and so-on. If all of them are false, the code in the else{} block is executed (note that "else" has no condition on it, not like "if" and "else if").

Secondly, I would say it's possible that your externally loaded script files might be messing with your variables "month" and "day", changing them so that the next condition returns true and the next file is loaded. Without an idea what's in those files though I can only guess.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
[&nbsp;]

You could do something like this and dump ALL of the if statements:


datafile = "h" + String.fromCharCode(month + 96) + String.fromCharCode((day % 15) +1) + ".js"
document.write("<script src='" + datafile + "><\/script>")}


Note that after the equal sign above there is a single quote followed by a double quote.

Totally untested and off the top of my head.

This should always call the correct file the first time.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
[&nbsp;]
Oops. Change the first line to:


datafile = "h" + String.fromCharCode(month + 48) + String.fromCharCode((day % 15) + 97) + ".js"


That should be closer to what you need.

This only works up to month 9. Since I don't know how you do 10 - 12, I can't refine this more.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
[&nbsp;]
And, of course, I forgot a single quote in the second line:


document.write("<script src='" + datafile + "'><\/script>")}


Single quotes are in red. Double in blue.


mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
[&nbsp;]
By the way, what happens on the 15th of every month?

From what I see in your code, you will NEVER call any of these js files on the 15th of any month.

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Hi

I agree with mmerlinn, that lengthy [tt]if[/tt] list is unmaintainable.

But I have to say, a few weeks would be enough to me to forget what I intended to do with those [tt]String.fromCharCode()[/tt] calls.

Personally I prefer it like this, helps me with later debugging :
Code:
datafile='h'+month+(day<15?'a':'b')+'.js'

Feherke.
 
[&nbsp;]
My ignorance of js is showing. LOL

mmerlinn


"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Steven Raymond
 
Thank you all was able to rewrite it with your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top