Let's say that someone decides to press F5 or the browser's reload button. Is that possible to open a specific url in the same tab?
Asked
Active
Viewed 2.1k times
1
-
Yes. But what tools have you? PHP, JS? – CoderPi Jan 28 '16 at 12:00
-
`Is that possible to open a specific url in the same tab?` You mean redirect them to another page? – Rory McCrossan Jan 28 '16 at 12:00
-
Possible duplicate of [Handling key-press events (F1-F12) using JavaScript and jQuery, cross-browser](http://stackoverflow.com/questions/424407/handling-key-press-events-f1-f12-using-javascript-and-jquery-cross-browser) – Brian Peacock Jan 28 '16 at 12:04
-
No it isn't possible... In fact there is surely some hacky ways but... – A. Wolff Jan 28 '16 at 12:08
-
@A.Wolff what are you talking about? – CoderPi Jan 28 '16 at 12:10
-
@CodeiSir Sorry, i mean without handling it server side. If one way only client side, i'd like to heard about it. Now maybe i completly misunderstood OP's expected behaviour. Here is client side solution for specific behaviour but still not just for refresh one: http://stackoverflow.com/a/23345263/1414562 – A. Wolff Jan 28 '16 at 12:10
-
@CodeiSir It's an HTML running JS and JQ. – Jim Jan 28 '16 at 12:15
-
@CodeiSir Please note regarding my previous comment link, jsFiddle doesn't like it... – A. Wolff Jan 28 '16 at 12:16
3 Answers
1
Like posted in comment, if you display prompt, this is possible using following ugly hack:
var a,b;
window.onbeforeunload = function (e) {
if (b) return;
a = setTimeout(function () {
b = true;
window.location.href = "//test.com";
}, 500);
return "Now you will be redirected to new page if choosing to stay there...";
}
window.onunload = function () {
clearTimeout(a);
}

A. Wolff
- 74,033
- 9
- 94
- 155
-
Yep! It works nice, still having the syntax error on DW but Dev Tool has nothing reported yet. Great! – Jim Jan 28 '16 at 12:45
-
-
@BrianPeacock So it deserves a downvote, right?!...??? And no, it cannot really be abused but sorry my english isn't as good as yours so maybe i misundertand what you are talking about... – A. Wolff Jan 28 '16 at 13:19
0
Use This
// To check if someone pressed f5 or leaving the page
window.onbeforeunload = function () {
//for changing the url
var newUrl = "http://www.someurl.com";
window.location.href = newUrl;
};

Anant Gulia
- 79
- 4
-
@Jim So you badly copied/pasted it. But anyway, wouldn't work for sure across browser – A. Wolff Jan 28 '16 at 12:19
-
-
@BrianPeacock But all (i guess) modern browsers block this behaviour, please try it on your side, is it working? – A. Wolff Jan 28 '16 at 12:21
-
Again, I'm having Dream Weaver and it comes with a syntax error. window.onbeforeunload = function () {var newUrl = "http://www.someurl.com"; window.location.assign(newUrl) }; – Jim Jan 28 '16 at 12:23
-
-
-
@ all of you comment makers that was incomplete new here at stackoverflow :) but seems like A. Wolff transformed it in to a working solution. – Anant Gulia Jan 28 '16 at 13:00
0
As noted in this Stack Overflow answer, the ShortCut library is designed to handle keyboard button events, including F-keys and arrow-buttons etc.
However, I must say that the idea of redirecting people to another page or site when they think they are refreshing the page is not only bad practice, but open to abuse.

Community
- 1
- 1

Brian Peacock
- 1,801
- 16
- 24
-
-
See the other question and then check out the documentation - seems you can inhibit/enhance inbuilt browser functionality with option objects. – Brian Peacock Jan 28 '16 at 12:16
-
But that's only for shortcut keys, not user clicking on browser button – A. Wolff Jan 28 '16 at 12:17
-
Did you even go to the ShortCut page? Scroll down and you'll see the a list of valid keys - including the F-keys. – Brian Peacock Jan 28 '16 at 12:21
-
I'm sorry but i'm talking about **clicking on browser refresh button**. How is it related to any shortcut key? – A. Wolff Jan 28 '16 at 12:23
-
You haven't understood that you can assign a function to any valid key with the ShortCut library - it's not just for shortcuts. As to your question - perhaps you could edit your post with the code that you have already tried? – Brian Peacock Jan 28 '16 at 12:48
-
But i'm not OP :) And you still haven't understood i'm talking about refreshing the page by clicking on browser refresh button, not by pressing any key... – A. Wolff Jan 28 '16 at 12:49
-
I'm sorry for any kind of confusion, i was thinking to be clear enough – A. Wolff Jan 28 '16 at 13:01