Jump to content

If month is december redirect, if not then dont HTML JS

WillLTT
Go to solution Solved by C2dan88,

I have expanded @Eigenvektor code to use absolute paths. Comments explains whats going

// if month is december
if (new Date().getMonth() == 11) {
	// if user is at site.com/ or site.com/index.html
	// redirect to site.com/char/index.html
	if (window.location.pathname == "/" || window.location.pathname == "/index.html") {
		window.location.replace("/chr/index.html");
	}

}
// month is not decemeber
else {
	// if user is at site.com/char/
	// redirect to site.com/index.html
	if (window.location.pathname.startsWith("/chr/")) {
		window.location.replace("/index.html");
	}
}

 

if (month=december):
   location.replace(site1.html);
else
   location.replace(site2.html);

how do i do something simmilar too that code, of course that code is dummy code and dont work. 

so If month december then goto html1 if not html2

Got it? 

 

im really noob at js, however ill try my best to understand.

Any help will be apprichiated. :D

Link to comment
Share on other sites

Link to post
Share on other sites

if (new Date().getMonth() == 11) {
  // It's December
} else {
  // It's not December
}

Note that months are 0-indexed in javascript, so January=0...December=11. This uses the time and time zone of the computer that it's running on.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

21 hours ago, colonel_mortis said:

if (new Date().getMonth() == 11) {
  // It's December
} else {
  // It's not December
}

 

if (new Date().getMonth() == 11) {
	location.replace("chr/index.html");
} else {
	location.replace("index.html");
}

it just sends me back and forwards between sites the specified sites.

Eventually it stops and the original site. im confused.

 

i wanted it to send you to chr/index.html if december and if not goto index.html

your code seems todo, but it wount stop redirecting.. until it stops and ends on original site

 

Link to comment
Share on other sites

Link to post
Share on other sites

13 minutes ago, WillLTT said:

if (new Date().getMonth() == 11) {
	location.replace("chr/index.html");
} else {
	location.replace("index.html");
}

it just sends me back and forwards between sites the specified sites.

Eventually it stops and the original site. im confused.

 

i wanted it to send you to chr/index.html if december and if not goto index.html

your code seems todo, but it wount stop redirecting.. until it stops and ends on original site

 

Does "chr/index.html" also have a redirect in its code? I probably stops, due to "too many redirects". Take a look at the browsers "Network" tab in developer tools.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Eigenvektor said:

Does "chr/index.html" also have a redirect in its code? I probably stops, due to "too many redirects". Take a look at the browsers "Network" tab in developer tools.

theyr both running from js.js file

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, WillLTT said:

theyr both running from js.js file

In other words, you open "index.html", it redirects you to "chr/index.html", which redirects you to "chr/index.html", which redirects you to "chr/index.html", ... at some point the browser will give up.

 

You probably need a safeguard, like so:

if (new Date().getMonth() == 11 && !location.endsWith("chr/index.html")) {
	location.replace("chr/index.html");
} else {
	location.replace("index.html");
}

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Eigenvektor said:

 


if (new Date().getMonth() == 11 && !location.endsWith("chr/index.html")) {
	location.replace("chr/index.html");
} else {
	location.replace("index.html");
}

 

still infinite redirect, check the source if you can make it work

 

thanks for helping me ?

 

also no i never sends me to chr/index.html because its not december yet.

its redirects me to index.html over and over again, but chr/index.html does not send me to index as its supposed todo beacause its not christmas

w1ll1am04.github.io-master.rar

Link to comment
Share on other sites

Link to post
Share on other sites

I won't be home for the rest of the day. I can have a look at it tomorrow, unless someone else has solved it in the meantime.

 

My first suggestion would be to add the same type of safeguard to index.html, i.e. only redirect if you're not already on that page.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

I have expanded @Eigenvektor code to use absolute paths. Comments explains whats going

// if month is december
if (new Date().getMonth() == 11) {
	// if user is at site.com/ or site.com/index.html
	// redirect to site.com/char/index.html
	if (window.location.pathname == "/" || window.location.pathname == "/index.html") {
		window.location.replace("/chr/index.html");
	}

}
// month is not decemeber
else {
	// if user is at site.com/char/
	// redirect to site.com/index.html
	if (window.location.pathname.startsWith("/chr/")) {
		window.location.replace("/index.html");
	}
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, C2dan88 said:

--snip--

its a little confusing beacause sometimes it works and sometimes not but i think it works!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×