Skip to content

Commit f371376

Browse files
author
zhangxinxu
committed
fix a big bug on mobile
fix a big bug on mobile, 'popstate' may excute twice when page load~
1 parent e6c5528 commit f371376

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

src/mobilebone.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@
122122
**/
123123
if (supportHistory == false) return Mobilebone;
124124

125+
/**
126+
* don't excute window.onpopstate when page load
127+
**/
128+
history.popstate = false;
125129

126130
/**
127131
* Function for transition
@@ -326,10 +330,15 @@
326330
if (url_push && /^#/.test(url_push) == false) {
327331
url_push = "#" + url_push;
328332
}
333+
329334
if (supportHistory && this.pushStateEnabled && options.history !== false && url_push) {
335+
// don't trigger 'popstate' events
336+
history.popstate = false;
330337
// if only pageIn, use 'replaceState'
331338
history[pageOut? "pushState": "replaceState"](null, document.title, url_push.replace(/^#/, "#&"));
332339
}
340+
// reset to popable state
341+
history.popstate = true;
333342

334343
// store page-id, just once
335344
if (!store[pageid]) {
@@ -782,7 +791,8 @@
782791
* Initialization. Load page according to location.hash. And bind link-catch events.
783792
**/
784793
Mobilebone.init = function() {
785-
if (hasInited == true) return 'Don\'t repeat initialization!';
794+
if (hasInited == true) return 'Don\'t repeat initialization!';
795+
786796
var hash = location.hash.replace("#&", "#"), ele_in = null;
787797
if (hash == "" || hash == "#") {
788798
this.transition(document.querySelector("." + this.classPage));
@@ -995,6 +1005,12 @@
9951005
* page change when history change
9961006
**/
9971007
window.addEventListener("popstate", function() {
1008+
console.log(history.popstate);
1009+
if (history.popstate == false) {
1010+
history.popstate = true;
1011+
return;
1012+
}
1013+
9981014
var hash = location.hash.replace("#&", "").replace("#", ""), page_in = null;
9991015
if (hash == "") {
10001016
// if no hash, get first page as 'page_in'

test/ajax-html/ajax-page.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<div class="page out" data-callback="optionsTest" data-onpagefirstinto="init" >
99
<a href="#pageHome" data-rel="back">返回</a>
1010
<button class="ajaxButton">点击我</button>
11+
<p class="result"></p>
1112
<script>
1213
console.log("loaded: " + Date.now());
1314
</script>

test/ajax-html/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@
3737
// console.log("点击的元素是:" + (options.target && options.target.outerHTML));
3838
// console.log("请求内容是:" + options.response);
3939
};
40-
var init = function() {
40+
var init = function(ele) {
4141
var button = document.querySelector(".ajaxButton");
4242
button.addEventListener("click", function() {
4343
console.log("clicked!");
4444
});
45+
ele.querySelector(".result").innerHTML += 'inited! ';
4546
};
4647
</script>
4748
</body>

0 commit comments

Comments
 (0)