bcherry-orig.html 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>WebKit is Dropping HTML5 "popstate" Events</title>
  6. <link rel="stylesheet" href="/static/lib/css/blueprint/blueprint.min.css" media="screen, projection" />
  7. <link rel="stylesheet" href="/static/lib/css/blueprint/print.min.css" media="print" />
  8. <!--[if lt IE 8]>
  9. <link rel="stylesheet" href="/static/lib/css/blueprint/ie.min.css" media="screen, projection">
  10. <![endif]-->
  11. <link href="/static/lib/css/bcherry.css" rel="stylesheet" media="screen" />
  12. <style>
  13. #n {
  14. font-size: 48px;
  15. }
  16. p {
  17. padding: 0 20px;
  18. }
  19. </style>
  20. <script type="text/javascript" src="../vendor/jquery.js"></script>
  21. <script type="text/javascript" src="../scripts/uncompressed/history.adapter.jquery.js"></script>
  22. <script type="text/javascript" src="../scripts/uncompressed/history.js"></script>
  23. </head>
  24. <body>
  25. <div id="n"></div>
  26. <p>There's a bug in the HTML5 "popstate" event, as implemented in WebKit (Safari and Chrome). View this page in one of those browsers. Your browser has had history entries added from #0 to #19 (you should start at #19). Hitting back/forward will navigate through these. On each URL, the large number above should reflect the hash value. If you hit back/forward quickly, you'll notice that your number gets out of sync with the URL. This is because WebKit is dropping popstate events (they are not firing). It seems to happen when outbound network requests are in progress when the user navigates in their browser happens. In this case, your browser is downloading an image that takes 1s to serve on every popstate, so you'll have to wait 1s between backs/forwards to have the feature work correctly. You could also cause constant network traffic by putting an image download in a setInterval, in which case your popstate events will never fire. This implementation simulates an AJAX application that makes a network request when you navigate between URLs using pushState/popstate. View the source for more info.</p>
  27. <p>This was filed as <a href="https://bugs.webkit.org/show_bug.cgi?id=42940">Bug 42940</a> with WebKit on July 24, 2010. The Firefox 4 beta does not have this bug, which is good news.</p>
  28. <p>This is put together by <a href="http://www.adequatelygood.com">Ben Cherry</a>. Ben is a front-end engineer at <a href="http://twitter.com/">Twitter</a>, and you can follow him at <a href="http://twitter.com/bcherry">@bcherry</a>.</p>
  29. <script>
  30. // Bind to popstate
  31. $(window).bind("popstate", function(e) {
  32. var State = e.state;
  33. // log that this event was fired, and the current URL
  34. if (window.console && window.console.log) {
  35. console.log("popstate", State, window.location.href);
  36. }
  37. // update the page
  38. $("#n").text(typeof State.n !== 'undefined' ? State.n : document.location.href);
  39. // Make an outbound image request that will take 1s. This request seems to be the cause of dropped popstates.
  40. // Removing this, or replacing it with something else, avoids the issue. Even if it's replaced with slow, blocking code (i.e. 1s of execution) events are not dropped.
  41. (new Image()).src = "http://www.bcherry.net/playground/pushstate.jpg";
  42. });
  43. // Seed the browser history
  44. for (var i = 0; i < 20; i++) {
  45. window.history.pushState({n:i}, i, "?" + i);
  46. $("#n").text(i);
  47. }
  48. </script>
  49. </body>
  50. </html>