2

Chrome and Safari doesn't trigger the HTML5 onpopstate event. Here is my example:

<!doctype html> 
<html> 
    <head> 
        <title></title> 
        <script src="http://code.jquery.com/jquery-latest.js"></script> 
        <script> 
            window.onpopstate = function(e) {
                document.getElementById("log").innerHTML = "HELLO";
            }
            $(function() {
                $("a").click(function(e) {
                    e.preventDefault();
                    window.history.pushState({page: $(this).index()}, $(this).html(), $(this).attr('href'));
                });
            });
        </script> 
    </head> 
    <body> 
        <a href="new-state">Foo</a> 
        <a href="other-state">Hallo</a> 
        <div id="log"></div> 
    </body> 
</html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
forberg
  • 63
  • 2
  • 6

1 Answers1

2

window.onpopstate() should fire on the initial page load, which counts as a history traversal. See this WebKit bug, which has been resolved in recent WebKit builds. (You code outputs HELLO as designed in Chrome 7.0.517.44, and presumably will do so in the next version of Safari.)

See also my (invalid) Chrome bug report for more discussion and links.

Robert Calhoun
  • 4,823
  • 1
  • 38
  • 34
  • They made more changes: http://stackoverflow.com/questions/4688164/window-bind-popstate This answer is no longer correct. – Ted Ballou Apr 18 '11 at 17:33