1

I have a gallery website with paintings that scrolls from left to right with overflow-x: scroll.

When the user clicks on a painting, the current scroll position is stored in localStorage and the detail page is shown. When the user clicks the back button, the scroll position is being set when the page is loaded.

I want this scroll position to be unset when the page is being loaded in a new tab or window. How can I tell if this happened?

Unsetting on page reload is already working. But opening the website in a new window does not count as a reload apparently.

I have tried reading the sessionStorage, but that doesn't do the trick.

if (performance.navigation.type == 1 || sessionStorage.isNewSession) {
   localStorage.setItem("scrollPositionHome", 0);
} 

performance.navigation.type sees it's a page reload, sessionStorage.isNewSession isn't doing much.

2 Answers2

0

You could right your own code to open links!

$('a').on('click', function(e) {
    // This will prevent the default behaviour 
    e.PreventDefault();
    // On Control click it's opened in a new window
    if (e.ctrlKey || e.metaKey){
    // Open in new window, you can already reset your localstorage here.
    localStorage.setItem("scrollPositionHome", 0);
    window.open(e.target.href);    
     } else {
    // Just a simple redirect
    window.location.href = e.target.href;
    }
});

NOTE: This is not tested! I will correct this if needed!

Wimanicesir
  • 4,606
  • 2
  • 11
  • 30
0

You seem to be mixing up sessionStorage and localStorage. You can find a small explanation following this link.

TL;DR: sessionStorage is per tab and localStorage will persist over multiple sessions. You could just empty your localStorage or use the sessionStorage in the first place.

If you could provide some code, we can give you a more thorough explanation / help.

Bronathan
  • 26
  • 1
  • 5