-1

I need your help, I have a website with 2 different versions ( day & night ) which are located on two different addresses, let's say : http://yashtest.com ( day ) and http://dark.yashtest.com ( night ). I don't know if it's something possible with javascript, but I wanted to load these versions based on the time of the day of the user.

EDIT

I tried this, but it reload the page constantly:

I'm new to Javascript, thank you for your patience.

var currentTime = new Date().getHours();
        if (7 >= currentTime && currentTime > 20) {
                window.location.href = "dark.yashtest.com";
        }
        else {
                window.location.href =  "yashtest.com";
        }
Yash
  • 27
  • 4
  • Please share the code which you have attempted. – Hassan Imam Mar 07 '18 at 15:30
  • Near-duplicates to this question: https://stackoverflow.com/questions/4358155/changing-background-based-on-time-of-day-using-javascript or https://stackoverflow.com/questions/31556085/change-text-daily-based-on-day-and-time-of-day Take any of those answers and replace the DOM manipulation with a window redirect. – Daniel Beck Mar 07 '18 at 15:37
  • @Yash have `dark.yashtest.com` only direct to `yashtest.com` and `yashtest.com` only direct to `dark.yashtest.com`. This way you will avoid the page constantly directing to itself – Tony Mar 07 '18 at 16:29

1 Answers1

1

Use a condition before directing to make sure the window is not already on the page you want to direct to.

Example:

// GET CURRENT TIME
var currentTime = new Date().getHours();

// CHECK DAY OR NIGHT
if (7 >= currentTime || currentTime > 20) {

    // CHECK IF NOT ALREADY ON NIGHT PAGE
    if(!window.location.href.includes("dark")){

        // DIRECT NIGHT
        window.location.href = "dark.yashtest.com";

    }

}else{

    // CHECK IF NOT ALREADY ON DAY PAGE
    if(window.location.href.includes("dark")){

        // DIRECT DAY
        window.location.href =  "yashtest.com";

    }

}
Tony
  • 2,890
  • 1
  • 24
  • 35
  • Thanks for your help but it still reload the page – Yash Mar 07 '18 at 16:51
  • @Yash try it with the changes I made. – Tony Mar 07 '18 at 16:58
  • Thanks again, I tried and it doesn't reload the page anymore, but unfortunately it doesn't load the 'night page' when trying to apply it at an earlier time for test – Yash Mar 07 '18 at 17:35
  • @Yash did you put the `!` in the front of the condition? I had missed it earlier – Tony Mar 07 '18 at 20:46
  • Yes I did, I don't understand what's wrong, I tried to reverse like this and it redirect to the night page whatever the hour : var currentTime = new Date().getHours(); if (7 >= currentTime && currentTime > 20) { if(!window.location.href.includes("yashtest.com")){ window.location.href = "yashtest.com"; } }else{ if(!window.location.href.includes("dark.yashtest.com")){ window.location.href = "dark.yashtest.com"; } } – Yash Mar 07 '18 at 21:32
  • @Yash maybe try doing a console log of what `window.location.href` is or maybe run `alert(window.location.href)` at the start of your script. – Tony Mar 08 '18 at 23:02
  • With alert(window.location.href) the page location is the 'Day page' with your original script, it didn't redirect to the 'Night page' ( when it's night of course ) – Yash Mar 12 '18 at 16:11
  • @Yash when you say its 'Day page' do you mean the alert contains the url `yashtest.com` exactly? – Tony Mar 12 '18 at 16:13
  • Yes that' it, sorry if I'm not clear ! Maybe there is a problem here ? : if (7 >= currentTime && currentTime > 20) Currently it's 17h here, so I tried like this to test it (7 >= currentTime && currentTime > 17) – Yash Mar 12 '18 at 16:14
  • @Yash change the `&&` to `||` – Tony Mar 12 '18 at 16:20
  • The alert is still yashtest.com :( – Yash Mar 12 '18 at 16:24
  • @Yash try doing an alert for `currentTime` – Tony Mar 12 '18 at 16:28
  • I added this to the script for time alert, and the hour is correct : alert(startTime()); function startTime(){ var today = new Date(); var h = today.getHours(); var m = today.getMinutes(); var s = today.getSeconds(); return [ h, m, s ].join(':') } – Yash Mar 12 '18 at 17:27
  • Well it work now, I don't know why but when I remove the alert and keep the time function, it works ! Thanks a lot for your help @Tony ! – Yash Mar 12 '18 at 17:29