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

Why doesn't this "if...else" statement work?

Status
Not open for further replies.

programmher

Programmer
May 25, 2000
235
US
Here is what I'm TRYING to do:

If today's date is less than or equal to 5, proceed to the next web page and allow the user to continue with their entries.

If today's date is greater than 5 (does not meet the first condition), display a message and redirect them to the home page.

Below is my code. It does not work the way it should. Can anyone tell me why and how I can debug it?



<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

transfer_location = &quot;Home.cfm&quot;;

var montharray=new Array(&quot;Jan&quot;,&quot;Feb&quot;,&quot;Mar&quot;,&quot;Apr&quot;,&quot;May&quot;,&quot;Jun&quot;,&quot;Jul&quot;,&quot;Aug&quot;,&quot;Sep&quot;,&quot;Oct&quot;,&quot;Nov&quot;,&quot;Dec&quot;);
var today=new Date();
var todayy=today.getYear();

if ((navigator.appName == &quot;Microsoft Internet Explorer&quot;) && (todayy < 3000))
todayy=&quot;20&quot; + todayy;
if (navigator.appName == &quot;Netscape&quot;)
todayy=2000 + todayy;

var todaym=today.getMonth();
var todayd=today.getDate();
var todaystring=montharray[todaym]+&quot; &quot;+todayd+&quot;, &quot;+todayy;


if (todayd <= 5)
document.location.href = &quot;Input.cfm&quot;;

else

alert(&quot;Sorry! You can only access this page the first five days of the month. You will be returned to the Home page.&quot;)
document.location.href = &quot;Home.cfm&quot;;



 
This worked:

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

transfer_location = &quot;Home.cfm&quot;;

var montharray=new Array(&quot;Jan&quot;,&quot;Feb&quot;,&quot;Mar&quot;,&quot;Apr&quot;,&quot;May&quot;,&quot;Jun&quot;,&quot;Jul&quot;,&quot;Aug&quot;,&quot;Sep&quot;,&quot;Oct&quot;,&quot;Nov&quot;,&quot;Dec&quot;);
var today=new Date();
var todayy=today.getYear();

if ((navigator.appName == &quot;Microsoft Internet Explorer&quot;) && (todayy < 3000))
todayy=&quot;20&quot; + todayy;
if (navigator.appName == &quot;Netscape&quot;)
todayy=2000 + todayy;

var todaym=today.getMonth();
var todayd=today.getDate();
var todaystring=montharray[todaym]+&quot; &quot;+todayd+&quot;, &quot;+todayy;


if (todayd <= 5)
window.location = &quot;Input.cfm&quot;;
alert(&quot;good&quot;)

else

alert(&quot;Sorry! You can only access this page the first five days of the month. You will be returned to the Home page.&quot;)
window.location = &quot;Home.cfm&quot;;
</script>
 
JaredN,

Thanks for the &quot;fix&quot;! It DID work; except, now it goes to my input page, then proceeds to go back to the home page. It's functioning as if it's continuing to process though the code and perform both instances of the conditional statement. Any thoughts on how I can remedy this?
 
Try replacing this with the last few lines:

if (todayd <= 5){
window.location = &quot;Input.cfm&quot;;
alert(&quot;good&quot;)
} else {
alert(&quot;Sorry! You can only access this page the first five days of the month. You will be returned to the Home page.&quot;)
window.location = &quot;Home.cfm&quot;;}

</script>

the reason is, I think that an if statement needs those brackets if it has more than one statement. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top