native.html 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <html>
  2. <head>
  3. <title>HTML5 History API Demo</title>
  4. </head>
  5. <body>
  6. <textarea id="log" style="width:100%;height:400px;margin:1em;"></textarea>
  7. <div id="url" style="border:1px black dotted;height:1em;margin:1em;"></div>
  8. <div id="buttons" style="margin:1em;"></div>
  9. <script type="text/javascript">
  10. var $url = document.getElementById('url'), $log = document.getElementById('log');
  11. window.onpopstate = function(event) {
  12. var message =
  13. "onpopstate: "+
  14. "location: " + location.href + ", " +
  15. "data: " + JSON.stringify(event.state) +
  16. "\n\n"
  17. ;
  18. $url.innerHTML = location.href;
  19. $log.innerHTML += message;
  20. console.log(message);
  21. };
  22. window.onhashchange = function(event) {
  23. var message =
  24. "onhashchange: "+
  25. "location: " + location.href + ", "+
  26. "hash: " + location.hash +
  27. "\n\n"
  28. ;
  29. $url.innerHTML = location.href;
  30. $log.innerHTML += message;
  31. console.log(message);
  32. };
  33. // Prepare Buttons
  34. var
  35. buttons = document.getElementById('buttons'),
  36. scripts = [
  37. 'history.pushState({state:1}, "State 1", "?state=1");',
  38. 'history.pushState({state:2}, "State 2", "?state=2");',
  39. 'history.replaceState({state:3}, "State 3", "?state=3");',
  40. 'location.hash = Math.random();',
  41. 'history.back();',
  42. 'history.forward();',
  43. 'document.location.href = document.location.href.replace(/[\#\?].*/,"");'
  44. ],
  45. buttonsHTML = ''
  46. ;
  47. // Add Buttons
  48. for ( var i=0,n=scripts.length; i<n; ++i ) {
  49. var _script = scripts[i];
  50. buttonsHTML +=
  51. '<li><button onclick=\'javascript:'+_script+'\'>'+_script+'</button></li>';
  52. }
  53. buttons.innerHTML = buttonsHTML;
  54. </script>
  55. </body>
  56. </html>