63
|
1 /**
|
|
2 * jQuery history event v0.1
|
|
3 * Copyright (c) 2008 Tom Rodenberg <tarodenberg gmail com>
|
|
4 * Licensed under the GPL (http://www.gnu.org/licenses/gpl.html) license.
|
|
5 */
|
|
6 (function($) {
|
|
7 var currentHash, previousNav, timer, hashTrim = /^.*#/;
|
|
8
|
|
9 var msie = {
|
|
10 iframe: null,
|
|
11 getDoc: function() {
|
|
12 return msie.iframe.contentWindow.document;
|
|
13 },
|
|
14 getHash: function() {
|
|
15 return msie.getDoc().location.hash;
|
|
16 },
|
|
17 setHash: function(hash) {
|
|
18 var d = msie.getDoc();
|
|
19 d.open();
|
|
20 d.close();
|
|
21 d.location.hash = hash;
|
|
22 }
|
|
23 };
|
|
24
|
|
25 var historycheck = function() {
|
|
26 var hash = msie.iframe ? msie.getHash() : location.hash;
|
|
27 if (hash != currentHash) {
|
|
28 currentHash = hash;
|
|
29 if (msie.iframe) {
|
|
30 location.hash = currentHash;
|
|
31 }
|
|
32 var current = $.history.getCurrent();
|
|
33 $.event.trigger('history', [current, previousNav]);
|
|
34 previousNav = current;
|
|
35 }
|
|
36 };
|
|
37
|
|
38 $.history = {
|
|
39 add: function(hash) {
|
|
40 hash = '#' + hash.replace(hashTrim, '');
|
|
41 if (currentHash != hash) {
|
|
42 var previous = $.history.getCurrent();
|
|
43 location.hash = currentHash = hash;
|
|
44 if (msie.iframe) {
|
|
45 msie.setHash(currentHash);
|
|
46 }
|
|
47 $.event.trigger('historyadd', [$.history.getCurrent(), previous]);
|
|
48 }
|
|
49 if (!timer) {
|
|
50 timer = setInterval(historycheck, 100);
|
|
51 }
|
|
52 },
|
|
53 getCurrent: function() {
|
|
54 return currentHash.replace(hashTrim, '');
|
|
55 }
|
|
56 };
|
|
57
|
|
58 $.fn.history = function(fn) {
|
|
59 $(this).bind('history', fn);
|
|
60 };
|
|
61
|
|
62 $.fn.historyadd = function(fn) {
|
|
63 $(this).bind('historyadd', fn);
|
|
64 };
|
|
65
|
|
66 $(function() {
|
|
67 currentHash = location.hash;
|
|
68 if ($.browser.msie) {
|
|
69 msie.iframe = $('<iframe style="display:none" src="javascript:false;"></iframe>').prependTo('body')[0];
|
|
70 msie.setHash(currentHash);
|
|
71 currentHash = msie.getHash();
|
|
72 }
|
|
73 });
|
|
74 })(jQuery);
|