history1.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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</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.object');
  31. goog.require('goog.string');
  32. </script>
  33. </head>
  34. <body>
  35. <h1>goog.History</h1>
  36. <p>This page demonstrates the goog.History object which can create new browser
  37. history entries without leaving the page. This version uses the hash portion of
  38. the URL to make the history state available to the user. These URLs can be
  39. bookmarked, edited, pasted in emails, etc., just like normal URLs. The browser's
  40. back and forward buttons will navigate between the visited history states.</p>
  41. <p>Try following the hash links below, or updating the location with your own
  42. tokens. Replacing the token will update the page address without appending a
  43. new history entry.</p>
  44. <p>
  45. Set #fragment<br>
  46. <a href="#first">first</a><br>
  47. <a href="#second">second</a><br>
  48. <a href="#third">third</a>
  49. </p>
  50. <p>
  51. Set Token<br>
  52. <button onclick="setToken('//\\\\/\\/\\');">//\\/\/\</button>
  53. <button onclick="setToken('{\'a\': \'123\', \'b\': \'456\'}');">
  54. {'a': '123', 'b': '456'}
  55. </button>
  56. <button onclick="setToken('!@#$%^&*()_+-={}[]\\|;\':&quot;,./<>?');">
  57. !@#$%^&*()_+-={}[]\|;':",./&lt;&gt;
  58. </button>
  59. <button onclick="setToken('%2F/foo');">%2F/foo</button>
  60. <button onclick="setToken('%20 02%');">%20&nbsp;&nbsp;&nbsp;02%</button>
  61. </p>
  62. <p>
  63. <input type="text" id="token_input" />
  64. <button onclick="setToken()">Set Token</button>
  65. <button onclick="replaceToken()">Replace Current Token</button>
  66. </p>
  67. <table><tr><td>
  68. <h3>The current history state:</h3>
  69. <div id="token_output"></div>
  70. </td></tr></table>
  71. <p>The state should be correctly restored after you
  72. <a href="http://www.google.com/">leave the page</a> and hit the back button.</p>
  73. <p>The history object can also be created so that the history state is not
  74. user-visible/modifiable.
  75. See <a href="history2.html#came from history 1">history2.html</a> for a demo.
  76. To see visible/modifiable history work when the goog.History code itself is
  77. loaded inside a hidden iframe,
  78. see <a href="history3.html#came from history 1">history3.html</a>.
  79. </p>
  80. <fieldset class="goog-debug-panel">
  81. <legend>Event Log</legend>
  82. <div id="log"></div>
  83. </fieldset>
  84. <script>
  85. var logger = goog.log.getLogger('demo');
  86. var logconsole = new goog.debug.DivConsole(goog.dom.getElement('log'));
  87. logconsole.setCapturing(true);
  88. var events = goog.object.getValues(goog.history.EventType);
  89. goog.log.info(logger, 'Listening for: ' + events.join(', ') + '.');
  90. var h = new goog.History();
  91. goog.events.listen(h, events, function(e) {
  92. goog.log.info(logger, goog.string.subs('dispatched: %s (token="%s", isNavigation=%s)',
  93. e.type, e.token, e.isNavigation));
  94. });
  95. goog.events.listen(h, goog.history.EventType.NAVIGATE, navCallback);
  96. h.setEnabled(true);
  97. function setToken(opt_val) {
  98. var input = goog.dom.getElement('token_input');
  99. h.setToken(opt_val || input.value);
  100. return false;
  101. }
  102. function replaceToken() {
  103. var input = goog.dom.getElement('token_input');
  104. h.replaceToken(input.value);
  105. }
  106. function navCallback(e) {
  107. var output = goog.dom.getElement('token_output');
  108. if (output) {
  109. var token = (e.token == null) ? 'null' : '\u201C' + e.token + '\u201D';
  110. goog.dom.setTextContent(output, token);
  111. }
  112. }
  113. </script>
  114. </body>
  115. </html>