Home | History | Annotate | Download | only in assets
      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             if (currentHash) {
     55               return currentHash.replace(hashTrim, '');
     56             } else {
     57               return "";
     58             }
     59         }
     60     };
     61 
     62     $.fn.history = function(fn) {
     63         $(this).bind('history', fn);
     64     };
     65 
     66     $.fn.historyadd = function(fn) {
     67         $(this).bind('historyadd', fn);
     68     };
     69 
     70     $(function() {
     71         currentHash = location.hash;
     72         if ($.browser.msie) {
     73             msie.iframe = $('<iframe style="display:none" src="javascript:false;"></iframe>').prependTo('body')[0];
     74             msie.setHash(currentHash);
     75             currentHash = msie.getHash();
     76         }
     77     });
     78 })(jQuery);
     79