JavaScript Page scrolls to top when closing modal using back button in mobile Safari page scroll down when clicking hrefu003d#div,page scrolls up

This is potentially a somewhat oddly specific question.

The situation:

I have a page with a position:fixed modal dialog (actually several, but that's irrelevant to the problem, as far as I've been able to determine). The modal opens when clicking a certain link. A script listens for hashchange events in order to close the modal when the user hits the back button.

The expected behaviour is that when back:ing out of the dialog, the page returns to the scroll position where it was before opening the modal. This happens in nearly every modern browser I've tested (desktop FF, Chrome, IE9/10/11 and Safari, and Chrome for Android).

On iPhone/mobile Safari, the page instead scrolls back to the top.

Test code

This is as reduced as I've been able to make it.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html, charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Modal test</title>
</head>
<body>

<h1>Header</h1>
<p>Enough<br>content<br>to<br>cause<br>some<br>scrolling</p>
<br><br><br><br><br><br><br>
<br><br><br><br><br><br><br>
<br><br><br><br><br><br><br>

<h1>Header</h1>
<p><a target='_blank' href="##popup" class="js-open-popup">Open popup</a></p>
<p>Enough<br>content<br>to<br>cause<br>some<br>scrolling<br>
<br><br><br><br><br><br>
<br><br><br><br><br><br>
<br><br><br><br><br><br>


<div id="#popup" style="background:#fff; width:300px; height:100px; border:2px solid #000; position:fixed; top:50px; left:50px; display:none">
    Modal dialog.<br>
    Hitting 'back' should close this modal.<br><br>

    <a target='_blank' href="javascript:void(0)" class="js-close-popup">Close modal</a>
</div>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
    (function($){
        $(document).ready(function(){
            $('.js-close-popup').click(function(ev){
                window.history.back();
                return false;
            });

            $('.js-open-popup').click(function(ev){
                $('#\\#popup').fadeIn(400);
            });
        });

        $(window).on('hashchange', function(){
            hash = window.location.hash.replace('#','');
            if(hash == '' || hash.lastIndexOf('#', 0) !== 0){
                $('#\\#popup').fadeOut(400);
            }
        });
    })(jQuery);
</script>

</body>
</html>

What I want to do is kill the scroll-to-top on iPhones, if at all possible without changing too much of the back-button logic for the popups (or breaking something in any other browsers).

I've searched SO for X number of hours but haven't been able to find a mention of this particular problem, or indeed a solution that works. Feel free to slap me on the fingers and point me in the right direction if I've missed an existing thread that answers my question.

EDIT: To clarify, opening the popup works as expected and does not cause any "auto-scroll".

Answer:1

I'm building Socket.IO's example chat project (with some modifications) and I've been trying to get people to connect with both localhost:3000 and 127.0.0.1:3000, but neither are working. Am I missing ...

I'm building Socket.IO's example chat project (with some modifications) and I've been trying to get people to connect with both localhost:3000 and 127.0.0.1:3000, but neither are working. Am I missing ...

  1. hosting socket server
  2. websocket server hosting
  3. hosting socket server c#

I am using jQuery UI for information display on desktops in tabs and have so for some time. I have added Responsive Tabs to Accordion to show the same information in accordion tabs for tablets and ...

I am using jQuery UI for information display on desktops in tabs and have so for some time. I have added Responsive Tabs to Accordion to show the same information in accordion tabs for tablets and ...

  1. jquery accordion tabs example
  2. jquery accordion tabs
  3. jquery accordion tab click event
  4. jquery accordion tab menu
  5. jquery tabs accordion responsive
  6. jquery accordion inside tabs
  7. jquery hide accordion tab
  8. jquery accordion select tab
  9. jquery accordion active tab
  10. jquery accordion open tab
  11. jquery accordion close tab
  12. jquery accordion first tab open
  13. jquery accordion remove tab
  14. jquery accordion disable tab
  15. jquery ui tabs accordion responsive
  16. jquery accordion close all tabs
  17. jquery accordion open all tabs
  18. accordion tabs jquery free download
  19. jquery ui accordion inside tabs
  20. jquery ui accordion in tabs

I am attempting to disable a button using an external JavaScript - activated by a submit button <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

I am attempting to disable a button using an external JavaScript - activated by a submit button <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

I'm new to HTML (just started learning at school) and I have a small question about classes and the ability to filter them. Basicially, I'm working on a page of football fixtures, which will look as ...

I'm new to HTML (just started learning at school) and I have a small question about classes and the ability to filter them. Basicially, I'm working on a page of football fixtures, which will look as ...

  1. create html class in javascript
  2. create html class
  3. create class html css
  4. js create html class
  5. create html helper class in mvc
  6. create html element with class javascript
  7. html create element class
  8. html create style class
  9. create div class html
  10. create new class html
  11. create custom class html
  12. html create dynamic class
  13. html create paragraph class
  14. html create pseudo-class
  15. html create element add class
  16. html create element set class