/    Sign up×
Community /Pin to ProfileBookmark

Stop a Script?

Does anyone know how to stop a script from running any further if a match is found to an If statement? For example, suppose your script said:

if (a < 10) document.write (“Less than 10”);
if (a <100) document.write (“Less than 100”);
if (a < 1000) document.write (“Less than 1000”);

But you only want it to write ONE statement, whichever one it hits first, not all of them.

[FONT=Courier New]if (a < 10) {
document.write (“Less than 10”);
break;}
if (a <100) {
document.write (“Less than 100”);
break;}
if (a < 1000) {
document.write (“Less than 1000”);
break;}[/FONT]

But of course break only works inside loops.

I know you could do this in a switch, but I’m wondering if it’s possible in an IF statement.

Thank you!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@LeeUAug 05.2005 — I believe this should work:

[CODE]if (a < 10) {
document.write ("Less than 10");
}
else if (a <100) {
document.write ("Less than 100");
}
else if (a < 1000) {
document.write ("Less than 1000");
}
else {
document.write ("Over 1000");
}[/CODE]
Copy linkTweet thisAlerts:
@-asx-authorAug 05.2005 — Thank you. I have this enormous script that is for a custom 404 page. 404 errors are sent to the script and generate a redirect and error message based on the URL the user was trying to reach. Since there are so many old URLs to redirect, I am trying to use minimal code by sticking with the simple IF statements rather than the IF...Else statements. But I suppose it's not that much extra code. So you can see what I mean, here's a chunk of the code, with most of the IF statements removed. . .

[FONT=Courier New]/* Initialize page title */

var strPageTitle = ("<h1>File Not Found</h1>");

/* Initialize strRequested with page attempted by user */

var strRequested = (location.pathname.toLowerCase()) ;

/* Build array with path segments that will identify the targeted page as belonging to a section formerly present on the old site.*/

var pathSeg = new Array ("isn_21-jul-2005.htm","/isambassador/ae/","/isambassador/ap/", "/isambassador/cs/", "/isambassador/faq/", "/isambassador/is/", "/isambassador/iss/", "/isambassador/mr/", "/isambassador/news/", "/isambassador/pa/");

/* Look for exact matches */

/* Added July 28, 2005 at roll-out of new site. */

if ( strRequested == "/isambassador/default.htm" ) document.location = "http://mikms.intranet.dow.com/isambassador/default.asp";

if ( strRequested == "/isambassador/news/isn_main.htm" ) document.location = "http://mikms.intranet.dow.com/isambassador/";

if ( strRequested == "/isambassador/cs/cs_main.htm" ) document.location = "http://mikms.intranet.dow.com/isambassador/services/";

if ( strRequested == "/isambassador/cs/cs_key.htm" ) document.location = "http://mikms.intranet.dow.com/isambassador/services/srv_cc_key.asp";

if ( strRequested == "/isambassador/cs/cs_cc1.htm" ) document.location = "http://mikms.intranet.dow.com/isambassador/communications/ct/c_ct.asp";

... dozens more like this ....

/* Look for old folders (Uses pathSeg array from above) */

for (count = 0; count < pathSeg.length; count++)

{

if (strRequested.indexOf(pathSeg[count]) > -1 )

{

break;

}

else if (count == pathSeg.length - 1)

{

count = count + 1;

}

}

switch (count)

{

case 0:

var strPageTitle = ("<h1>Shared Services Listening Sessions</h1>");

var strError = ("<p><b>We're sorry,</b> but all of the Listening Sessions for August have been filled. We thank you for your interest. Additional Listening Sessions will be scheduled throughout the year.</p>");

break;

case 1:
var strError = ("<p><b>Please Note:</b> The <i><b>Ambassador's Edge</b></i> has been retired. News stories and the Leadership Column are available from the I/S Ambassador home page. Please wait eight seconds to be redirected.</p><p>If possible, please also reset the bookmark or link that led you to this page.</p>");
setTimeout("document.location = 'http://mikms.intranet.dow.com/isambassador/'",8000);
break;


... a few more like this

default:
var strError = ("<p>We’re sorry, but the page you are requesting is not available. You may have typed the address incorrectly, followed a broken link, or used an out-dated shortcut. If possible, please reset the bookmark or link that led you to this page.</p>");

}[/FONT]
×

Success!

Help @-asx- 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 4.30,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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