Tips for ensuring Google Chrome and iOS will remember scroll position in a div upon returning

One issue that stands out is the scroll container being a div instead of the body. This is causing problems with Google Chrome and iOS not remembering the scroll position of the div when users navigate to another page and return.

The relevant CSS code looks like this, with the scroll container defined as #container:

* {
    margin: 0;
    padding: 0;
    }

html {
    height: 100%;
    }

body {
    height: 100%;
    overflow: hidden;
    }

#container {
    height: 100%;
    overflow: auto;
    }

You can view a demo on jsFiddle here. To reproduce the issue in Google Chrome and iOS, try scrolling the content halfway (or any other visible length), then click on a hyperlink to navigate to another page before using the browser's Back button to return.

Is there a straightforward solution to make Google Chrome and iOS remember the scroll position like Firefox does?


== EDIT responding to an answer ==

I have included a simple .html file below to test if any code within the onbeforeunload event works:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <script>
        window.onbeforeunload = function () {
            alert('Are you sure');
        }
    </script>
</head>
<body>
    <div id="container">
        <p><strong><a href="http://stackoverflow.com/">Go to Stack Overflow</a></strong>. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas laoreet imperdiet diam, in sodales velit porta eget.</p>
    </div>
</body>
</html>

Answer â„–1

Check out this Demo with URL and Session https://jsfiddle.net/w2wkcx0e/6/

Explore the Demo Based on URL https://jsfiddle.net/w2wkcx0e/3/

Experience the Live Demo https://jsfiddle.net/w2wkcx0e/1/

If you leave the page, your position will be saved and reloaded when you return.

Let me know if it doesn't function as expected in all desired browsers.

if (localStorage.scrollPos != undefined)
    $('#container').scrollTop(localStorage.scrollPos);

window.onbeforeunload = function () {
    localStorage.setItem("scrollPos", $('#container').scrollTop());
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to restrict the width of a column in a v-data-table?

I'm trying to restrict the width of the Video title column in my headers by using the width: '30%' property. However, despite setting this value, the column width still exceeds the specified percentage. I also attempted using maxWidth: &apos ...

Strategies for aligning Bootstrap tooltip text vertically in the center

Is there a way to perfectly center the text vertically inside a Bootstrap tooltip? I've tried adding this CSS but it still leaves about 1px or 2px of space at the bottom: .tooltip-inner { text-align: center !important; justify-content: center !im ...

CSS: How come this inline-block div is not appearing on the same line as intended?

I've been tinkering with using display:inline-block on my div elements, and I'm puzzled as to why my two inner divs aren't appearing on the same line. Both divs have a width of 200px, while their parent div is set at 400px. If I switch the ...

Tips for creating floating and right-aligned notifications using CSS in Vue.js

Struggling to position my basic notification component in the top right corner. Here is my HTML: <div class="notifications"> <p>{{ notification.message }}</p> </div> This is my notifications class: .notifications { position: ...

Is the CSS 'position: absolute' applied to the element immediately following the body tag

Quoting from msdn: "The object is placed relative to the position of its parent element—or to the body element if the parent element is not positioned" Let's consider a scenario where I set a div with specific dimensions to have a bottom value ...

Having trouble getting the Click Element with CSS selector to function properly in Google Tag Manager?

How can I detect a click on the specified element by using the "Click Element" variable in Google Tag Manager? <a href="https://amazon.in/product-id" target="_blank" class="amazon-btn"> <div> <span&g ...

Customize the look and feel of your website using jQuery's

Is there a way to use an icon from the jQuery Themeroller as a toggle button in a Ruby on Rails project? I want the icon to switch colors when clicked, but I'm not sure how to accomplish this. Any advice on how to achieve this functionality? ...

How can I boost the amount of posts on a category page?

It seems that Blogger has a limit of 20 unique labels per page according to various sources. Here is the URL where the restriction is in place: www.BLOG.com/search/label/This-is-the-label-page-that-displays-only-20-posts/ Is there a workaround out there ...

Text exceeding boundaries within a horizontal flexbox (accordion)

Need help with a flexbox (accordion) issue. The demo below showcases multiple groups of content, each contained within a cell in a horizontal flexbox. However, when the content within a group is extensive, it overflows the flexbox causing display problems. ...

Can you designate a specific Google font for each language on a webpage?

Is there a way to use two different Google fonts, one for English characters and another for Thai characters, on a single page? While it's possible to achieve this using the @font-face syntax by specifying unicode character ranges, Google fonts do no ...

Picture goes missing from slideshow

I am currently using a CSS + Javascript slideshow on my website, inspired by an example from the W3Schools website. Here is the code I have adapted for my web application: $(document).ready(function() { var slideIndex = 1; function showSlides(n) { ...

It vanishes as soon as you move your cursor away during the animation

I created a button component with text animation, but I'm encountering an issue. When I hover over the button, the animation works smoothly. However, if I quickly move my cursor away or unhover in the middle of the animation, the text disappears unex ...

Utilizing PHP to dynamically add other PHP CSS files to a CSS file

Something truly peculiar has come to my attention. An unusual discovery was made: there exists a CSS file that is, in fact, a PHP file with a header specifying itself as CSS. This CSS file also includes other PHP files posing as CSS files, all of which ha ...

Dynamic font sizing in CSS allows text on a webpage to

I am working on creating a dynamic screen using AngularJS. Within this screen, there are objects with a specific size: .item { margin: auto; margin-bottom: 10px; width: 11vw; height: 11vw; text-overflow: ellipsis; overflow: hidden; } These i ...

Edge and Internet Explorer fail to recognize the :focus CSS selector

I am utilizing CSS to make our placeholders jump up when clicked inside, but Edge is not recognizing the CSS. It works fine on every other browser except for Edge. I've tried various solutions but none seem to work on Edge. Any thoughts on what might ...

Placing a box in the center of its parent container using CSS positioning

Check out this code snippet : HTML <div class="app-cont"> <div class="app-head"> Additional Comments : </div> <div class="app-main"> balallalalalallalalalala <br/> jaslnflkasnlsnlk ...

Tips for populating a row in the Bootstrap grid with the assistance of the mr or ml classes

I've come across a roadblock here. The issue arises when attempting to fill the Bootstrap row while also utilizing mr (or ml). Initially, I attempted to calculate the numbers so that they add up to 12. In my 3-column layout, I have a column of 5, fol ...

Nested tables with repeated data using AngularJS' ng-repeat functionality

As a newcomer to angularjs and web-development, I am facing a challenge with using ng-repeat to display data in a complex table structure, like this: A B C D abc pqr xyz std Here is the code snippet: <div class="table-responsive"> ...

How come the vertical positioning of my HTML button in a <td> is affected when I include an image?

https://i.sstatic.net/AetS4.pngI typically include only text on my buttons, which maintains the vertical alignment of the buttons within the <td> element (Code-1). However, when I add an image to the button, the alignment is thrown off. The height ...

Switch the position of the tab from the left side of the page to the right side

I currently have a tab positioned on the left side of my screen, but I am now looking to move it to the right side and adjust its vertical positioning. Below is the code that places the tab on the left side: CSS #feedback { position: fixed; ...