Skip to content Skip to sidebar Skip to footer

Javascript Redirect Based On Date

I want the user to be directed to bar.html on the first of the month, gjb.html on the second of the month, guerr.html on the third of the month and error.html on other dates. What

Solution 1:

Just needs to be a proper equality check, not the assignment operator you are using:

<html><scripttype="text/javascript">var currentDate = newDate().getDate();
    if (currentDate === 1)
        window.location = "bar.html";
    elseif (currentDate === 2))
        window.location = "gjb.html";
    elseif (currentDate === 3))
        window.location = "guerr.html";
    elsewindow.location = "error.html";
</script></html>

I suggest === over == because that does a proper type check, and you are guaranteed to get Integers so this is a safer check. When in doubt, ===.

Solution 2:

You are setting the date with currentTime.getDate() = 1. Try currentTime.getDate() == 1 or currentTime.getDate() === 1. (I dont use js all the time, but '=' is wrong).

Solution 3:

try using double equals (==). The single equals operator signifies assignment while the double equals signifies a comparison.

Example:

// Assign a value to a int a = 1;

// Assign a value to b int b = 2;

// See whether or not a is equal to b

if( a == b ) {
        System.out.println("They're equal!");
}
else {
         System.out.println("They're not equal!");
} 

Post a Comment for "Javascript Redirect Based On Date"