I am trying to store the previous url data in a back button so that the page can be shared directly without losing that information. I tried with javascript using the document referrer and with cookies but it doesn't work if the page is accessed directly. Thanks in advance.
Asked
Active
Viewed 1,547 times
0
-
Instead of saving so many things, create a random has and store data/session info on server. On request, check for hash and pass necessary data and process accordingly. This will ensure your site works even when users disable cookies – Rajesh Jan 19 '18 at 05:14
-
if you can share little more details, it will be great. I don't have user logins and most random hash generation articles are related to storing of user login sessions. Thanks – Rahul Jan 20 '18 at 06:23
3 Answers
1
Try something like this.
When a user visits the webpage, run this:
// When the user closes the window, save the url in a localStoage
window.onbeforeunload = () => {
localStorage.setItem('previousUrl', document.URL);
};
Once they revisit again, run this function:
// When the user open the webpage, it will show the data saved last time
function getPreviousUrl() {
return document.referrer ? document.referrer : localStorage.getItem('previousUrl');
}
// Return the url saved last time
getPreviousUrl();

K.K Designs
- 688
- 1
- 6
- 22

xianshenglu
- 4,943
- 3
- 17
- 34
-
This is working if the same user is coming to the page. If someone else whom the page has been shared tries to access it, it doesn't work. – Rahul Jan 20 '18 at 06:19
-
e,,,think about that i share you a url and you can visit the url and even know the history website i have been visited?is that possible and reasonable? – xianshenglu Jan 20 '18 at 06:32
-
if i share you the url ,can you get the previous url according to the document.referrer? if not,you can change the url by changing the hash(#) if the url is shared by someone and the previous url is added behind the present url.if you want to share data between users ,that data should be saved in the server or the url. that is all i think – xianshenglu Jan 20 '18 at 06:43
0
you can use jquery insted of store data in cookies.
jQuery(document).ready(function($) {
if (window.history && window.history.pushState) {
window.history.pushState('forward', null, './#forward');
$(window).on('popstate', function() {
alert('Back button clicked...');
});
}
});

Sachin Shah
- 4,503
- 3
- 23
- 50
-
This is working if the same user is coming to the page. If someone else whom the page has been shared tries to access it, it doesn't work. – Rahul Jan 20 '18 at 06:19
0
window.history.pushState(data, title, targetUrl);

Richard Padre
- 141
- 6
-
This is working if the same user is coming to the page. If someone else whom the page has been shared tries to access it, it doesn't work. – Rahul Jan 20 '18 at 06:19
-
If that’s the case, I think this is something you should do on the server side. You cannot accomplish this on the client side (Javascript) as each user have their own session. You can save the history of the URL’s in a database table or in the application memory, although that could potentially cause threading issues. – Richard Padre Jan 20 '18 at 06:24