history2.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  5. Use of this source code is governed by the Apache License, Version 2.0.
  6. See the COPYING file for details.
  7. -->
  8. <head>
  9. <title>goog.History #2</title>
  10. <link rel="stylesheet" href="css/demo.css">
  11. <style>
  12. table {
  13. border: 1px solid #666;
  14. background: lightblue;
  15. margin: 1em auto;
  16. }
  17. td {
  18. text-align: center;
  19. padding: 0 0.5em 0.5em 0.5em;
  20. }
  21. </style>
  22. <script src="../base.js"></script>
  23. <script>
  24. goog.require('goog.History');
  25. goog.require('goog.debug.DivConsole');
  26. goog.require('goog.dom');
  27. goog.require('goog.events');
  28. goog.require('goog.history.EventType');
  29. goog.require('goog.log');
  30. goog.require('goog.string');
  31. </script>
  32. </head>
  33. <body>
  34. <h1>goog.History #2</h1>
  35. <p>This page demonstrates the goog.History object which can create new browser
  36. history entries without leaving the page. This version maintains the history
  37. state internally, so that states are not visible or editable by the user, but
  38. the back and forward buttons can still be used to move between history states.
  39. </p>
  40. <p>Try setting a few history tokens using the buttons and box below, then hit
  41. the back and forward buttons to test if the tokens are correctly restored.</p>
  42. <button onclick="setToken('one')">State 1</button>
  43. <button onclick="setToken('two')">State 2</button>
  44. <button onclick="setToken('three')">State 3</button>
  45. <button onclick="setToken('four')">State 4</button><br>
  46. <button onclick="setToken('//\\\\/\\/\\');">//\\/\/\</button>
  47. <button onclick="setToken('{\'a\': \'123\', \'b\': \'456\'}');">
  48. {'a': '123', 'b': '456'}
  49. </button>
  50. <button onclick="setToken('!@#$%^&*()_+-={}[]\\|;\':&quot;,./<>?');">
  51. !@#$%^&*()_+-={}[]\|;':",./&lt;&gt;
  52. </button>
  53. <button onclick="setToken('%2F/foo');">%2F/foo</button>
  54. <button onclick="setToken('%20 02%');">%20&nbsp;&nbsp;&nbsp;02%</button>
  55. <p>
  56. <input type="text" id="token_input" />
  57. <button onclick="javascript:setToken()">Set Token</button>
  58. <button onclick="replaceToken()">Replace Current Token</button>
  59. </p>
  60. <table><tr><td>
  61. <h3>The current history state:</h3>
  62. <div id="token_output"></div>
  63. </td></tr></table>
  64. <p>The state should be correctly restored after you
  65. <a href="http://www.google.com/">leave the page</a> and hit the back button.</p>
  66. <p>The history object can also be created so that the history state is visible
  67. and modifiable by the user. See <a href="history1.html">history1.html</a> for a
  68. demo.</p>
  69. <fieldset class="goog-debug-panel">
  70. <legend>Event Log</legend>
  71. <div id="log"></div>
  72. </fieldset>
  73. <script>
  74. var logger = goog.log.getLogger('demo');
  75. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  76. logconsole.setCapturing(true);
  77. var events = goog.object.getValues(goog.history.EventType);
  78. goog.log.info(logger, 'Listening for: ' + events.join(', ') + '.');
  79. var h = new goog.History(true, 'history_blank.html');
  80. goog.events.listen(h, events, function(e) {
  81. goog.log.info(logger, goog.string.subs('dispatched: %s (token="%s", isNavigation=%s)',
  82. e.type, e.token, e.isNavigation));
  83. });
  84. goog.events.listen(h, goog.History.EventType.NAVIGATE, navCallback);
  85. h.setEnabled(true);
  86. function setToken(opt_token) {
  87. var input = goog.dom.getElement('token_input');
  88. h.setToken(opt_token || input.value);
  89. }
  90. function replaceToken(opt_token) {
  91. var input = goog.dom.getElement('token_input');
  92. h.replaceToken(opt_token || input.value);
  93. }
  94. function navCallback(e) {
  95. var output = goog.dom.getElement('token_output');
  96. if (output) {
  97. var token = (e.token == null) ? 'null' : '\u201C' + e.token + '\u201D';
  98. goog.dom.setTextContent(output, token);
  99. }
  100. }
  101. </script>
  102. </body>
  103. </html>