Reputation: 11
For my react application I would like for example the current URL is https://localhost:3000.com?page=10.
Before the user refreshes the page manually, I would like to change the page number to 1 so that it redirects the user back to page 1.
Have tried with window.onbeforeunload
and adding eventListener
with beforeunload
combined with window.location.search = '?page=5'
(using setTimeout
works sometimes, most of the time it doesn't).
None of them work.
Upvotes: 1
Views: 52
Reputation: 3305
If you are using Express, you could use Express Session, setting a session variable req.session.sendToOnReload
, then the user reloads the page in the same session it will redirect them to this page.
var url = require('url')
router.get('/', function(req, res) {
if (req.session.sendToOnReload === undefined) {
req.session.sendToOnReload = url.format({
pathname: req.session.sendToOnReload,
query: req.query
});
res.end();
} else {
res.redirect(req.session.sendToOnReload);
}
}
In this code, when the page is first loaded, it sets the page to which the user will be redirected when they reload the page. Then, the second time the user requests the page (by reloading) the server redirects them to the page specified on the first page load.
Upvotes: 1