-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.breakpage.js
91 lines (74 loc) · 1.76 KB
/
jquery.breakpage.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
(function($) {
$.breakpage = function( options ){
//pushstate extend
(function(original) {
history.pushState = function(state) {
original.apply(this, arguments);
change();
//return original.apply(this, arguments);
};
})(history.pushState);
function initOnPopStatefunction(){
window.onpopstate = function(){
change();
}
}
function change(){
setPreviousURL();
setURL();
if(!isSameURL()){
tryBreakPage();
if(debug)console.log(previousURL +" ->->-> "+ URL);
}
}
function tryBreakPage(){
if(isSameURL()){
return;
}
functions.forEach(function(element, index){
if(checkFrom(element.from) && checkTo(element.to)){
element.do();
}
});
}
function isSameURL(){
return (URL === previousURL)? true : false ;
}
function checkFrom(from){
return (previousURL.match(from) !== null)?true:false;
}
function checkTo(to){
return (URL.match(to) !== null)?true:false;
}
function setPreviousURL(){
previousURL = URL;
}
function setURL(){
URL = document.URL.slice(root_length);
}
//vars
var URL,previousURL,root,root_length,functions,debug;
//funcitons get
var defaults = {
'root' : "",
'degug' : false,
'functions' : ""
};
var setting = $.extend( defaults, options );
//var functions = options.functions;
console.log(setting);
function init(){
//can use pushState
if(!'pushState' in history){ return; }
root = (setting.root === "") ? document.location.origin : root.length;
root_length = root.length;
functions = setting.functions;
debug = setting.debug;
initOnPopStatefunction();
setURL();
setPreviousURL();
}
//initalize
init();
}
})(jQuery);