1 |
efrain |
1 |
YUI.add('history-hash-ie', function (Y, NAME) {
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Improves IE6/7 support in history-hash by using a hidden iframe to create
|
|
|
5 |
* entries in IE's browser history. This module is only needed if IE6/7 support
|
|
|
6 |
* is necessary; it's not needed for any other browser.
|
|
|
7 |
*
|
|
|
8 |
* @module history
|
|
|
9 |
* @submodule history-hash-ie
|
|
|
10 |
* @since 3.2.0
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
// Combination of a UA sniff to ensure this is IE (or a browser that wants us to
|
|
|
14 |
// treat it like IE) and feature detection for native hashchange support (false
|
|
|
15 |
// for IE < 8 or IE8/9 in IE7 mode).
|
|
|
16 |
if (Y.UA.ie && !Y.HistoryBase.nativeHashChange) {
|
|
|
17 |
var Do = Y.Do,
|
|
|
18 |
GlobalEnv = YUI.namespace('Env.HistoryHash'),
|
|
|
19 |
HistoryHash = Y.HistoryHash,
|
|
|
20 |
|
|
|
21 |
iframe = GlobalEnv._iframe,
|
|
|
22 |
win = Y.config.win;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Gets the raw (not decoded) current location hash from the IE iframe,
|
|
|
26 |
* minus the preceding '#' character and the hashPrefix (if one is set).
|
|
|
27 |
*
|
|
|
28 |
* @method getIframeHash
|
|
|
29 |
* @return {String} current iframe hash
|
|
|
30 |
* @static
|
|
|
31 |
*/
|
|
|
32 |
HistoryHash.getIframeHash = function () {
|
|
|
33 |
if (!iframe || !iframe.contentWindow) {
|
|
|
34 |
return '';
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
var prefix = HistoryHash.hashPrefix,
|
|
|
38 |
hash = iframe.contentWindow.location.hash.substr(1);
|
|
|
39 |
|
|
|
40 |
return prefix && hash.indexOf(prefix) === 0 ?
|
|
|
41 |
hash.replace(prefix, '') : hash;
|
|
|
42 |
};
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Updates the history iframe with the specified hash.
|
|
|
46 |
*
|
|
|
47 |
* @method _updateIframe
|
|
|
48 |
* @param {String} hash location hash
|
|
|
49 |
* @param {Boolean} replace (optional) if <code>true</code>, the current
|
|
|
50 |
* history state will be replaced without adding a new history entry
|
|
|
51 |
* @protected
|
|
|
52 |
* @static
|
|
|
53 |
* @for HistoryHash
|
|
|
54 |
*/
|
|
|
55 |
HistoryHash._updateIframe = function (hash, replace) {
|
|
|
56 |
var iframeDoc = iframe && iframe.contentWindow && iframe.contentWindow.document,
|
|
|
57 |
iframeLocation = iframeDoc && iframeDoc.location;
|
|
|
58 |
|
|
|
59 |
if (!iframeDoc || !iframeLocation) {
|
|
|
60 |
return;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
Y.log('updating history iframe: ' + hash + ', replace: ' + !!replace, 'info', 'history');
|
|
|
64 |
|
|
|
65 |
if (replace) {
|
|
|
66 |
iframeLocation.replace(hash.charAt(0) === '#' ? hash : '#' + hash);
|
|
|
67 |
} else {
|
|
|
68 |
iframeDoc.open().close();
|
|
|
69 |
iframeLocation.hash = hash;
|
|
|
70 |
}
|
|
|
71 |
};
|
|
|
72 |
|
|
|
73 |
Do.before(HistoryHash._updateIframe, HistoryHash, 'replaceHash', HistoryHash, true);
|
|
|
74 |
|
|
|
75 |
if (!iframe) {
|
|
|
76 |
Y.on('domready', function () {
|
|
|
77 |
var lastUrlHash = HistoryHash.getHash();
|
|
|
78 |
|
|
|
79 |
// Create a hidden iframe to store history state, following the
|
|
|
80 |
// iframe-hiding recommendations from
|
|
|
81 |
// http://www.paciellogroup.com/blog/?p=604.
|
|
|
82 |
//
|
|
|
83 |
// This iframe will allow history navigation within the current page
|
|
|
84 |
// context. After navigating to another page, all but the most
|
|
|
85 |
// recent history state will be lost.
|
|
|
86 |
//
|
|
|
87 |
// Earlier versions of the YUI History Utility attempted to work
|
|
|
88 |
// around this limitation by having the iframe load a static
|
|
|
89 |
// resource. This workaround was extremely fragile and tended to
|
|
|
90 |
// break frequently (and silently) since it was entirely dependent
|
|
|
91 |
// on IE's inconsistent handling of iframe history.
|
|
|
92 |
//
|
|
|
93 |
// Since this workaround didn't work much of the time anyway and
|
|
|
94 |
// added significant complexity, it has been removed, and IE6 and 7
|
|
|
95 |
// now get slightly degraded history support.
|
|
|
96 |
Y.log('creating dynamic history iframe', 'info', 'history');
|
|
|
97 |
|
|
|
98 |
iframe = GlobalEnv._iframe = Y.Node.getDOMNode(Y.Node.create(
|
|
|
99 |
'<iframe src="javascript:0" style="display:none" height="0" width="0" tabindex="-1" title="empty"/>'
|
|
|
100 |
));
|
|
|
101 |
|
|
|
102 |
// Append the iframe to the documentElement rather than the body.
|
|
|
103 |
// Keeping it outside the body prevents scrolling on the initial
|
|
|
104 |
// page load (hat tip to Ben Alman and jQuery BBQ for this
|
|
|
105 |
// technique).
|
|
|
106 |
Y.config.doc.documentElement.appendChild(iframe);
|
|
|
107 |
|
|
|
108 |
// Update the iframe with the initial location hash, if any. This
|
|
|
109 |
// will create an initial history entry that the user can return to
|
|
|
110 |
// after the state has changed.
|
|
|
111 |
HistoryHash._updateIframe(lastUrlHash || '#');
|
|
|
112 |
|
|
|
113 |
// Listen for hashchange events and keep the iframe's hash in sync
|
|
|
114 |
// with the parent frame's hash.
|
|
|
115 |
Y.on('hashchange', function (e) {
|
|
|
116 |
lastUrlHash = e.newHash;
|
|
|
117 |
|
|
|
118 |
if (HistoryHash.getIframeHash() !== lastUrlHash) {
|
|
|
119 |
Y.log('updating iframe hash to match URL hash', 'info', 'history');
|
|
|
120 |
HistoryHash._updateIframe(lastUrlHash);
|
|
|
121 |
}
|
|
|
122 |
}, win);
|
|
|
123 |
|
|
|
124 |
// Watch the iframe hash in order to detect back/forward navigation.
|
|
|
125 |
Y.later(50, null, function () {
|
|
|
126 |
var iframeHash = HistoryHash.getIframeHash();
|
|
|
127 |
|
|
|
128 |
if (iframeHash !== lastUrlHash) {
|
|
|
129 |
Y.log('updating URL hash to match iframe hash', 'info', 'history');
|
|
|
130 |
HistoryHash.setHash(iframeHash);
|
|
|
131 |
}
|
|
|
132 |
}, null, true);
|
|
|
133 |
});
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
}, '3.18.1', {"requires": ["history-hash", "node-base"]});
|