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