Created
December 3, 2009 09:52
-
-
Save anarcher/248034 to your computer and use it in GitHub Desktop.
history.right.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/javascript" src="./right-src.js"></script> | |
<script type="text/javascript" src="./history.right.js"></script> | |
<script type="text/javascript"> | |
var observer = new Observer(); | |
observer.observe('change', function(hash) { | |
alert(hash); | |
}); | |
var aHistory = new History({ | |
binding: observer | |
}); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setScope(callback, scope) { | |
return function() { | |
return callback.apply(scope, arguments); | |
}; | |
}; | |
var History = new Class({ | |
extend: { | |
Version: '0.1', | |
}, | |
include: Options, | |
Options: { | |
binding: null, | |
defaultHash: '' | |
}, | |
initialize: function(options) { | |
this.setOptions(options); | |
var hash = (top.location.hash == '' || top.location.hash == '#') ? this.options.defaultHash : top.location.hash; | |
if (Browser.IE) { | |
document.write('<iframe id="msieframe" style="display: none;"></iframe>'); | |
this.iframe = $("msieframe").contentWindow.document; | |
this.iframe.open(); | |
this.iframe.close(); | |
this.iframe.location.hash = top.location.hash; | |
this.hash = this.iframe.location.hash; | |
} | |
else { | |
this.hash = hash; | |
} | |
this.change(hash); | |
this.intervalId = setInterval(setScope(this.checkHash,this), 200); | |
}, | |
setLocation: function(hash) { | |
top.location.hash = hash; | |
this.hash = top.location.hash; | |
if (Browser.IE) { | |
this.iframe.open(); | |
this.iframe.close(); | |
this.iframe.location.hash = this.hash; | |
} | |
this.change(hash); | |
}, | |
checkHash: function() { | |
if (Browser.IE) { | |
var f = $("msieframe"); | |
var iframe = f.contentDocument || f.contentWindow.document; | |
if (this.hash != iframe.location.hash ) { | |
this.hash = iframe.location.hash; | |
this.hash = ( this.hash == '#' ) ? '' : this.options.defaultHash; | |
top.location.hash = this.hash; | |
var hash = this.hash ? this.hash : this.options.defaultHash; | |
this.change(hash); | |
} | |
return; | |
} | |
if(this.hash != top.location.hash) { | |
this.hash = top.location.hash; | |
var hash = this.hash ? this.hash : this.options.defaultHash; | |
this.change(hash); | |
} | |
}, | |
/* private methods */ | |
change: function(hash) { | |
if (this.options.binding) { | |
if(isArray(this.options.binding)) { | |
for(i = 0; i < this.options.binding.length; i++) { | |
this.options.binding[i].fire('change',hash); | |
} | |
} | |
else { | |
this.options.binding.fire('change',hash); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment