Repository navigation
Expand file tree
/
Copy pathhashlistener2.js
More file actions
174 lines (156 loc) · 3.79 KB
/
Copy pathhashlistener2.js
File metadata and controls
174 lines (156 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Hash Listener 2.0
*
* ************************************************************
* Basic object to allow updating the hash part of the document location.
* Mozilla always adds an entry to the history but for IE we add an optional
* flag whether to add an entry to the history and if this is set an iframe is
* used to support this behavior (this is on by default).
*
* When the hash value changes onHashChanged is called. Override this to do
* your own callbacks.
*
* Usage: Include script
* Override onHashChanged: hashListener.onHashChanged = fn
*
* Browser Support: FireFox 3, Opera 9+, IE 6+, Chrome, Safari 4
* * Not tested: Konkeror, old Safari, old Firefox
*
* **********************************************************
* @author Marcelo Eden
* @copyright (c) 2000 - 2009 OS!Schools
* @license BSD
* @created 18/03/2009
* @updated 20/03/2009
*/
var hashListener = {
browser: null,
isOk: true,
hash: "",
/**
* Check hash
*/
check: function () {
var h = location.hash;
if (h != this.hash) {
this.updateHistory(h);
this.onHashChanged();
}
},
/**
* Start Action
*/
init: function () {
// Check if can go on
this.loadBrowser();
if (!this.isOk) {
return false;
}
// Self
var self = this;
// Dinamical Changes
if ("onpropertychange" in document && "attachEvent" in document) {
document.attachEvent("onpropertychange", function () {
if (event.propertyName == "location") {
self.check();
}
});
}
// poll for changes of the hash
window.setInterval(function () { self.check() }, 150);
return true;
},
/**
* Set Hash
*/
setHash: function (s) {
location.hash = s;
},
/**
* Get Hash
*/
getHash: function () {
return location.hash;
},
/** Important!
* trigger onHashChanged Event
* Need to be overwriten
*/
onHashChanged: function () { },
/* *******************************************
* CrossBrowser
* ***************************************** */
loadBrowser: function () {
var nav = navigator.userAgent.toLowerCase();
this.isOk = true;
// IE
if (/msie/.test(nav)) {
this.browser = 'ie';
// IE need a frame trick
this.createFrame();
}
// Firefox
else if (/firefox/.test(nav)) {
this.browser = 'firefox';
}
// Genko
else if (/safari/.test(nav)) {
this.browser = 'gecko';
}
// Genko
else if (/chrome/.test(nav)) {
this.browser = 'chrome';
}
// Genko
else if (/opera\/9/.test(nav)) {
this.browser = 'opera9';
}
// Genko
else if (/gecko/.test(nav)) {
this.browser = 'gecko';
}
// Else other Element
else{
this.browser = '???';
this.isOk = false;
}
return this.isOk;
},
updateHistory: function(h){
// IE
if (this.browser == 'ie') {
this.writeFrame(h);
}
// All Browser
this.hash = h;
},
/* *************************************
* IE Hacks
* *********************************** */
createFrame: function () {
var frame = document.createElement("iframe");
frame.id = "state-frame";
frame.style.display = "none";
document.body.appendChild(frame);
this.writeFrame( location.hash );
},
/**
* Write content to IFrame
*/
writeFrame: function (s) {
var f = document.getElementById("state-frame");
var d = f.contentDocument || f.contentWindow.document;
d.open();
d.write('<script>window.hash2 = "'+ s +'";window.onload = parent.hashListener.syncFrameHash;</script>');
d.close();
},
/**
* Syncronise Document Hash with IFrame Hash
*/
syncFrameHash: function () {
var s = this.hash2;
if (s != location.hash) {
location.hash = s;
}
}
};