-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.js
160 lines (132 loc) · 3.6 KB
/
push.js
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
(function(exports) {
var cache = {}
function loadJs(src, callback) {
if (!cache[src]) {
cache[src] = {
state: 'init',
queue: []
}
}
if (cache[src].state == 'ready') {
callback()
} else {
cache[src].queue.push(callback)
}
var doc = document;
var head = doc.getElementsByTagName('head')[0];
var s = doc.createElement('script');
var re = /^(?:loaded|complete|undefined)$/;
s.onreadystatechange =
s.onload =
s.onerror = function() {
if (re.test(s.readyState)) {
cache[src].state = 'ready'
var queue = cache[src].queue
for (var i = queue.length - 1; i >= 0; i--) {
queue[i]()
queue.pop()
}
s.onload = s.onerror = s.onreadystatechange = null
s = null
}
}
s.src = src
s.async = true
head.insertBefore(s, head.firstChild)
}
exports.loadJs = loadJs
})(this);
loadJs('', function() {
(function($) {
//必须支持pushstate
var support =
window.history && window.history.pushState && window.history.replaceState &&
!navigator.userAgent.match(/((iPod|iPhone|iPad).+\bOS\s+[1-4]\D|WebApps\/.+CFNetwork)/)
var cache = {}
var request = function(url, callback, isCache) {
var defaultJump = function() {
location.href = url
}
var success = function(data) {
try {
if (isCache) {
cache[url] = data
}
callback && callback(data)
} catch (e) {
defaultJump()
}
}
if (isCache) {
cache[url] && success(cache[url])
}
$.ajax({
url: url,
error: defaultJump,
success: success
})
}
var getContent = function(data, opts) {
var body = data.match(opts.contentReg || /<body>[\s\r\n]+([\s\S]+)<\/body>/i)[1]
var title = data.match(opts.titleReg || /<title>([\s\S]+)<\/title>[\s\r\n]+/i)[1]
return {
body: body,
title: title
}
}
var setContent = function(content, opts) {
$(opts.container).html(content.body)
document.title = content.title
}
var pushState = function(state) {
history.pushState(state, state.title, state.url)
}
$.fn.ajaxLoadPage = function(opts) {
if (!support) {
return this
}
if (!opts.container) {
opts.container = this
}
window.onpopstate = function(event) {
var state = event.state
if (!state) {
return
}
request(state.url, function(data) {
var content = getContent(data, opts)
setContent(content, opts)
opts.callback && opts.callback()
}, opts.cache)
}
//first init, replace current state
history.replaceState({
url: location.href,
title: document.title
}, document.title)
return this.on('click', opts.selector || 'a', function(event) {
var link = event.currentTarget
if (location.protocol !== link.protocol || location.hostname !== link.hostname) {
return
}
event.preventDefault()
var url = link.href
request(url, function(data) {
var content = getContent(data, opts)
setContent(content, opts)
pushState({
url: url,
title: content.title
})
opts.callback && opts.callback()
}, opts.cache)
})
}
})(jQuery)
$('#container').ajaxLoadPage({
cache: true,
callback: function() {
window.DUOSHUO && window.DUOSHUO.init()
}
})
})