| 1 |
/*
|
| 2 |
* jQuery history plugin
|
| 3 |
*
|
| 4 |
* Copyright (c) 2006 Taku Sano (Mikage Sawatari)
|
| 5 |
* Licensed under the MIT License:
|
| 6 |
* http://www.opensource.org/licenses/mit-license.php
|
| 7 |
*
|
| 8 |
* Modified by Lincoln Cooper to add Safari support and only call the callback once during initialization
|
| 9 |
* for msie when no initial hash supplied.
|
| 10 |
*/
|
| 11 |
|
| 12 |
|
| 13 |
jQuery.extend({
|
| 14 |
historyCurrentHash: undefined,
|
| 15 |
|
| 16 |
historyCallback: undefined,
|
| 17 |
|
| 18 |
historyInit: function(callback){
|
| 19 |
jQuery.historyCallback = callback;
|
| 20 |
var current_hash = location.hash;
|
| 21 |
|
| 22 |
jQuery.historyCurrentHash = current_hash;
|
| 23 |
if(jQuery.browser.msie) {
|
| 24 |
// To stop the callback firing twice during initilization if no hash present
|
| 25 |
if (jQuery.historyCurrentHash == '') {
|
| 26 |
jQuery.historyCurrentHash = '#';
|
| 27 |
}
|
| 28 |
|
| 29 |
// add hidden iframe for IE
|
| 30 |
$("body").prepend('<iframe id="jQuery_history" style="display: none;"></iframe>');
|
| 31 |
var ihistory = $("#jQuery_history")[0];
|
| 32 |
var iframe = ihistory.contentWindow.document;
|
| 33 |
iframe.open();
|
| 34 |
iframe.close();
|
| 35 |
iframe.location.hash = current_hash;
|
| 36 |
}
|
| 37 |
else if ($.browser.safari) {
|
| 38 |
// etablish back/forward stacks
|
| 39 |
jQuery.historyBackStack = [];
|
| 40 |
jQuery.historyBackStack.length = history.length;
|
| 41 |
jQuery.historyForwardStack = [];
|
| 42 |
|
| 43 |
jQuery.isFirst = true;
|
| 44 |
}
|
| 45 |
jQuery.historyCallback(current_hash.replace(/^#/, ''));
|
| 46 |
setInterval(jQuery.historyCheck, 100);
|
| 47 |
},
|
| 48 |
|
| 49 |
historyAddHistory: function(hash) {
|
| 50 |
// This makes the looping function do something
|
| 51 |
jQuery.historyBackStack.push(hash);
|
| 52 |
|
| 53 |
jQuery.historyForwardStack.length = 0; // clear forwardStack (true click occured)
|
| 54 |
this.isFirst = true;
|
| 55 |
},
|
| 56 |
|
| 57 |
historyCheck: function(){
|
| 58 |
if(jQuery.browser.msie) {
|
| 59 |
// On IE, check for location.hash of iframe
|
| 60 |
var ihistory = $("#jQuery_history")[0];
|
| 61 |
var iframe = ihistory.contentDocument || ihistory.contentWindow.document;
|
| 62 |
var current_hash = iframe.location.hash;
|
| 63 |
if(current_hash != jQuery.historyCurrentHash) {
|
| 64 |
|
| 65 |
location.hash = current_hash;
|
| 66 |
jQuery.historyCurrentHash = current_hash;
|
| 67 |
jQuery.historyCallback(current_hash.replace(/^#/, ''));
|
| 68 |
|
| 69 |
}
|
| 70 |
} else if ($.browser.safari) {
|
| 71 |
if (!jQuery.dontCheck) {
|
| 72 |
var historyDelta = history.length - jQuery.historyBackStack.length;
|
| 73 |
|
| 74 |
if (historyDelta) { // back or forward button has been pushed
|
| 75 |
jQuery.isFirst = false;
|
| 76 |
if (historyDelta < 0) { // back button has been pushed
|
| 77 |
// move items to forward stack
|
| 78 |
for (var i = 0; i < Math.abs(historyDelta); i++) jQuery.historyForwardStack.unshift(jQuery.historyBackStack.pop());
|
| 79 |
} else { // forward button has been pushed
|
| 80 |
// move items to back stack
|
| 81 |
for (var i = 0; i < historyDelta; i++) jQuery.historyBackStack.push(jQuery.historyForwardStack.shift());
|
| 82 |
}
|
| 83 |
var cachedHash = jQuery.historyBackStack[jQuery.historyBackStack.length - 1];
|
| 84 |
if (cachedHash != undefined) {
|
| 85 |
jQuery.historyCurrentHash = location.hash;
|
| 86 |
jQuery.historyCallback(cachedHash);
|
| 87 |
}
|
| 88 |
} else if (jQuery.historyBackStack[jQuery.historyBackStack.length - 1] == undefined && !jQuery.isFirst) {
|
| 89 |
// back button has been pushed to beginning and URL already pointed to hash (e.g. a bookmark)
|
| 90 |
// document.URL doesn't change in Safari
|
| 91 |
if (document.URL.indexOf('#') >= 0) {
|
| 92 |
jQuery.historyCallback(document.URL.split('#')[1]);
|
| 93 |
} else {
|
| 94 |
var current_hash = location.hash;
|
| 95 |
jQuery.historyCallback('');
|
| 96 |
}
|
| 97 |
jQuery.isFirst = true;
|
| 98 |
}
|
| 99 |
}
|
| 100 |
} else {
|
| 101 |
// otherwise, check for location.hash
|
| 102 |
var current_hash = location.hash;
|
| 103 |
if(current_hash != jQuery.historyCurrentHash) {
|
| 104 |
jQuery.historyCurrentHash = current_hash;
|
| 105 |
jQuery.historyCallback(current_hash.replace(/^#/, ''));
|
| 106 |
}
|
| 107 |
}
|
| 108 |
},
|
| 109 |
historyLoad: function(hash){
|
| 110 |
var newhash;
|
| 111 |
|
| 112 |
if (jQuery.browser.safari) {
|
| 113 |
newhash = hash;
|
| 114 |
}
|
| 115 |
else {
|
| 116 |
newhash = '#' + hash;
|
| 117 |
location.hash = newhash;
|
| 118 |
}
|
| 119 |
jQuery.historyCurrentHash = newhash;
|
| 120 |
|
| 121 |
if(jQuery.browser.msie) {
|
| 122 |
var ihistory = $("#jQuery_history")[0];
|
| 123 |
var iframe = ihistory.contentWindow.document;
|
| 124 |
iframe.open();
|
| 125 |
iframe.close();
|
| 126 |
iframe.location.hash = newhash;
|
| 127 |
jQuery.historyCallback(hash);
|
| 128 |
}
|
| 129 |
else if (jQuery.browser.safari) {
|
| 130 |
jQuery.dontCheck = true;
|
| 131 |
// Manually keep track of the history values for Safari
|
| 132 |
this.historyAddHistory(hash);
|
| 133 |
|
| 134 |
// Wait a while before allowing checking so that Safari has time to update the "history" object
|
| 135 |
// correctly (otherwise the check loop would detect a false change in hash).
|
| 136 |
var fn = function() {jQuery.dontCheck = false;};
|
| 137 |
window.setTimeout(fn, 200);
|
| 138 |
jQuery.historyCallback(hash);
|
| 139 |
// N.B. "location.hash=" must be the last line of code for Safari as execution stops afterwards.
|
| 140 |
// By explicitly using the "location.hash" command (instead of using a variable set to "location.hash") the
|
| 141 |
// URL in the browser and the "history" object are both updated correctly.
|
| 142 |
location.hash = newhash;
|
| 143 |
}
|
| 144 |
else {
|
| 145 |
jQuery.historyCallback(hash);
|
| 146 |
}
|
| 147 |
}
|
| 148 |
});
|