Ask coding questions

← Back to all posts
How to redirect my website to my home page when a user press the refresh button of the website
Coder2404 (361)

How to redirect my website to my home page when a user press the refresh button of the browser.

I want to know how to redirect my website to my home page when a user presses the refresh button of my website in html.

Comments
hotnewtop
NinjaCoderz (22)

usually when a user refreshes they dont generally want to be sent back to the home page because they will have to go back to the page they where on why dont you make a header and put a logo on the top left and make it a link

like this
<a href = "index.html"><img src = "lol.png"></a> ←this is a fake image i made up

JakeHu2020 (21)

I was thinking about this question for quite some time, but I finally got an answer: with javascript local storage
1: so make a file called redirect.js
2: go to the page you want to redirect on load, then paste this code at the <head> element:

<script src="redirect.js"></script>

3: open redirect.js then type in var redirect_place= and then the place you want it to redirect in strings
4: then paste this code in that file:

if(typeof(localStorage)!=undefined){if(localStorage.getItem('ffb')==0){localStorage.setItem('ffb',1)}else{localStorage.setItem('ffb',0);location.href=redirect_place;}}};

if my code isn't working, try typing it in (not paste).

Coder2404 (361)

@JakeHu2020 I’ll try it, I’ll tell you if it works

Baconman321 (1058)

@Coder2404 reply to this comment so as not to ping anyone else...

Coder2404 (361)

@Baconman321 oh and is this link blocked?
https://deadsimplechat.com/6pHzYu-Cw
If not please join and the pass is “Coder2404”
If it’s blocked then......

GatewayDuckYT (47)
<body onload='refresh()'>
<script>
var refresh = 1
if(refresh === 1) {
loction.reload()
}
KENNETHTRIPP (50)

onbeforeunload = () => {document.location.href = [your home page url];} maybe?

KENNETHTRIPP (50)

@Coder2404 No, you can't only use HTML. But you can do it in an HTML, CSS, JS repl.

Coder100 (16869)

How about using iframes? This way, when you refresh, all that page changes you would programmatically add would be gone.

For example:

<a onclick="... todo make it so that the iframe shows ...">click</a>
<iframe></iframe>
Coder2404 (361)

@Coder100 what do you mean exactly?

Coder2404 (361)

@Coder100 how did you do that? I’m confused...

Coder2404 (361)

@Coder100 yeah, just that I don’t know which line of code is the part when you reload the page the text disappears...

xxpertHacker (865)

You cannot intercept the refresh button, browsers don't have it, and keyboards don't have such a button.

You can, however, intercept key presses, specificially when they've pressed "Ctrl+R," this requires scripting, not HTML.

Baconman321 (1058)

@xxpertHacker You can click the reload button at the top of the page tho...
(Near the backward/forward navigation stuff)

I honestly think it's a bad practice as it's just plain frustrating. Also, they are making an SPA from the looks of it, so when they go to another "section" it just pulls up a new div and stuff with content on it. When they refresh it's automatically reset because they use Jquery (from the looks of it) to change the content.

Of course, I could be wrong they showed me a demo of their website sooooo...

xxpertHacker (865)

@Baconman321 Ohhhh, that button, the "on page" one, okay.
Time for globaThis.addEventListener("beforeunload", ...)

Baconman321 (1058)

@xxpertHacker I thought of that too, but I don't think they want the "leave this page" thing.

Also, for some reason I don't know how to make the "leave this page" popup show on addEventListener only on onbeforeunload because you have to return a value or whatever on the onbeforeunload for it to show up and it doesn't do that for the addEventListener... :/

xxpertHacker (865)

@Baconman321 e.returnValue exists.

Assign something to it.

xxpertHacker (865)

@Coder2404 Yeah, you really shouldn't be doing this, think of your users.

Baconman321 (1058)

@Coder2404 Right now I'm having a problem with repl (probably my firewall, soooo).
Maybe once that gets fixed then I can help.

Whoops, I replied to the wrong comment lol.

Baconman321 (1058)

@xxpertHacker Oh I never knew about returnValue!

Coder2404 (361)

@Baconman321 Tomorrow when are you available? We could do it at that time..What is your problem?I could try to fix it?

xxpertHacker (865)

@Coder2404 Just don't do it, it's unlikely to be possible, and if you pull it off you'll make a lot of users unhappy.

Baconman321 (1058)

@Coder2404 It's on my network, so I don't think that's possible. It's getting fixed right now tho...

Coder2404 (361)

@xxpertHacker but why does this website work?
https://coder2195.vercel.app
When you press the “coder2195 games” then reload it, it works for them.

Baconman321 (1058)

@Coder2404 Ye.
I have school now tho...

Coder2404 (361)

@Baconman321 me too, I’m remote and I’m splitting my screen...Can’t you split your screen?

Baconman321 (1058)

@Coder2404 Yes I can, but I need to focus on class right now :(

Coder2404 (361)

@Baconman321 Me too, what subject are you doing?
I’m doing....writing.....
(I’m not trying to distract you)

KENNETHTRIPP (50)

@Coder2404 That's because the URL never changes when you press "coder2195 games"

Coder2404 (361)

@Baconman321 You like science? I don’t. I like writing..sorta
:D

xxpertHacker (865)

@Coder2404 That is a SPA, not a multi-page website.

If you can reload to the users last position, that is better than a full restart.

xxpertHacker (865)

@Baconman321
@Coder2404

Jokes on you:

CS class > writing + science

> true

Baconman321 (1058)

@xxpertHacker CS is amazing but I feel jealous of the people contributing so much to science!

I feel like science contributes so much to CS to the point where without other sciences we wouldn't have CS or computers at all!

Coder2404 (361)

@Baconman321 I only sometimes like science..

xxpertHacker (865)

@Baconman321 The blueprints for the first computer were written by a mathematician. The first program was written by a bored female (forgot who exactly :/), iirc.

xxpertHacker (865)

@Coder2404 Store data, reset states. It's complicated.

Baconman321 (1058)

@xxpertHacker Yeah. Still, without science we wouldn't have efficient computers (think miniaturization of processors via the microcircuit).

We'd probably still be stuck with the first (or second) generation computers!

xxpertHacker (865)

@Baconman321 Well, without... um... farmers, the scientists wouldn't have food.

Agriculture FTW!

Without writing, the scientists wouldn't be able to write stuff and share knowledge.

We could keep going up the dependency chain, shall we?

Baconman321 (1058)

@xxpertHacker You literally said that CS is superior to science.

In reality almost everything relies on each other.

xxpertHacker (865)

@Baconman321
Ha, science relies on CS now though.
Agriculture relies on CS too.

Yes, just about everything depends on the other thing.

But I was referring to the classes in the beginning.

class mathematics extends ... {}
class science extends mathematics {}
class computer_science extends science {}
Baconman321 (1058)

@xxpertHacker Oh ok (phew).

yes computers are basically science but with 0's and 1's.

If you don't like logic you will hate CS (at its core).

xxpertHacker (865)

@Baconman321

If you don't like logic you will hate CS (at its core).

Umm, *looks at Pythoners*, *looks the other way, the Replers*, are you sure about that?

xxpertHacker (865)

@Baconman321 Most Pythoners and Replers are illogical

Baconman321 (1058)

@xxpertHacker LOL.

I'm suuuure you know a whole lot about science.

If you're so naive sure of yourself then why don't you go realize life is more complicated than ya think take organic chemistry where you become even more naive have to memorize tons of compounds (btw there are over 15 million compounds in organic chemistry).

xxpertHacker (865)

@Baconman321 Organic chemistry is just a subset of chemisty that focuses way too much on Carbon :/

There are a infinate possible combinations of atoms to create new compounds, and new (albeit, unstable) atoms are likely being syntesised as we speak, allowing more possibly compunds.

Of course, I'm just a naive child on Repl though.

Baconman321 (1058)

@xxpertHacker Yes, I know about organic chemistry (a bit).

Infinite combinations of atoms? I think you mean molecules (and reactions, don't forget reactions, even though they focus more on molecules interacting with other molecules), although molecules are made up of atoms (anyone over 6th grade should know that TBH).

You think it's way too much about carbon?

It's mainly focused on hydrocarbons, so don't forget hydrogen as well (most of the compounds contain at least a carbon-hydrogen bond, although it could contain other elements)!

xxpertHacker (865)

@Baconman321 Damn, I just said compounds were made up of atoms instead of molecules.

Literally nothing you've said is new to me.

Baconman321 (1058)

@xxpertHacker Good, I expect no less from u :D

Also, a compound is a molecule made up of atoms of different elements.

Uhm, do you know decoherence in quantum computing (I bet you'll say yes then right after that look it up)?

xxpertHacker (865)

@Baconman321

(I bet you'll say yes then right after that look it up)?

Ahh, ofc, that's how I do everything; how'd you know?

Uhm, do you know decoherence in quantum computing

Yes.

(I'm looking it up rn, please wait...)

Seriously, no, I haven't researched anything about quantum computing in years, and vaguely remember entanglement and whatnot (I knew the specifics way better). I used to be way more into quantum computing, quantum mechanics and related stuff, but haven't been at all lately, or at least this year.

Baconman321 (1058)

@xxpertHacker

Nothing you said is new to me

Uhm, but you look it up after I tell u, so no I am teaching u?

Then again u teach me too

:D

xxpertHacker (865)

@Baconman321

Uhm, but you look it up after I tell u, so no I am teaching u?

Wdym? I was being sarcastic when I said that I always say "yes," then search stuff up afterward :/

Baconman321 (1058)

@xxpertHacker Oh ok.

I still think I taught u some stuff :D

Baconman321 (1058)

@xxpertHacker Decoherence and such :D
What about the no-copy theorem?

U know that?

xxpertHacker (865)

@Baconman321 How did you teach me what quantum decoherence is? I still don't know what it is, and honestly don't care to look into it myself.

Coder2404 (361)

@Baconman321 hey, sorry to bother you but please try to check you email/gmail.

Baconman321 (1058)

@xxpertHacker It's basically a loss of information as it travels over a distance.

Like a game of telephone.

:D

Baconman321 (1058)

@Coder2404 Can't now...
About 3-4 CST is when I can...

xxpertHacker (865)

@Baconman321 Seems like a loss of coherence in the system as operations are parallelized.

Baconman321 (1058)

@Coder2404 Because that's when I have access to a computer that I can use my gmail acc on.

Baconman321 (1058)

@xxpertHacker Wdym parallelized?
It's when things transferred over long distances (I think qubits).

xxpertHacker (865)

@Baconman321 I was thinking of how with more qubits = more parallelization, therefore more speed, more power, & more decohesion.

Coder2404 (361)

@Baconman321 can you check you gmail? I sent you something

Baconman321 (1058)

@Coder2404 Ok.

I can't do gmail on fridays or saturdays...

:/

Baconman321 (1058)

@Coder2404 Maybe.

I can only do things for an hour so I play video games at that time.

If you have discord you can send me a friend request. My portfolio says what my number is so look there.

Coder2404 (361)

@Baconman321 I know this seems dumb but...
What is discord?

Coder2404 (361)

@Baconman321 hey, is your gmail account blocked?

Baconman321 (1058)

@Coder2404 Only on my main. I can chat now, but I'd rather not...

:/

Coder2404 (361)

@Baconman321 I don’t know what is discord..
What is discord?

xxpertHacker (865)

@Coder2404 Btw, y'all've been on my comment for the last 100 or so comments.

Also, just search us Discord, it's the first thing on literially every single search engine.

Coder2404 (361)

@Baconman321 oh that!
Um I don’t have discord then, because I can’t download an app...

Baconman321 (1058)

@Coder2404 It's online, nothing required to be installed (although you can install their app if you wanted too)

Coder2404 (361)

@Baconman321 Then how do I do discord?

Baconman321 (1058)

@Coder2404 Oh, wait you can't use it :(
You have to be over 13...
If you are tho, just go to discordapp.com and I believe it will ask you to signup (there must be a link somewhere on the page).

Coder2404 (361)

@Baconman321 Which I’m NOT over 13. D:

Coder2404 (361)

@Baconman321 and I can’t see a link..

Baconman321 (1058)

@Coder2404 oOf.

Uhm then, I don't know. Google docs isn't necessarily the best way and half the time I won't be able to chat...

Also, please don't keep on emailing me, it's confusing...

:/

xxpertHacker (865)

@Coder2404 Discord is a web application. https://discordapp.com
But can you two make a new comment chain on this post so I don't get notified of these comments?

Baconman321 (1058)

@Coder2404 Oh, and also... let's try to not use this for chat (at least not on this comment) because I think it's pinging xxperthacker...

Coder2404 (361)

@Baconman321 ok, I won’t email you, so is it fine if I talk to you in repl.it? And we could use https://deadsimplechat.com/getstarted, if that’s fine with you..

Baconman321 (1058)

@Coder2404 I don't think I can chat, really....
:/

It's bound to be blocked sooner or later. Besides, I am actually quite busy most days...
But, read the comment I put down saying we shouldn't reply to xxperthacker's comment. I made another comment that you should reply to... (the comment is saying that you should reply to "this" comment (the comment I am talking about))

sammightyt (24)

Couldn't you just add a home button?

Coder2404 (361)

@sammightyt I want it to direct to the home page when a user RELOADS it.

Coder2404 (361)

@sammightyt but do you know how to do it in html?

Baconman321 (1058)

@Coder2404 You can't.

HTML is a markup language. It doesn't handle logic.

You would have to use JavaScript...

Also, I don't recommend this as it is confusing to users (and annoying).

sammightyt (24)

@Coder2404 I don't think you can do it in html

Coder2404 (361)

@Baconman321 here is an example for how I want my website to be
https://coder2195.vercel.app
(Btw this is NOT my website, this is for demonstrating)
When you press on the “Coder2195 games” and you reload the page it goes to the home page, how do you do that?

Baconman321 (1058)

@Coder2404 It already does that?

Oh, I see what you want. You want an SPA (single page app).

Just hide the main content and pull up some stuff on your website. Then when you reload it resets back to the original content.

Coder2404 (361)

@Baconman321 See you tomorrow- I mean talk to you tomorrow!