Last active
October 13, 2015 20:29
-
-
Save wezoalves/4251445 to your computer and use it in GitHub Desktop.
navigation url hash
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
/** | |
* Author: Weslley Alves <weslley.tec@gmail.com> | |
* Author URI: http://wezo.com.br | |
* Version: 0.0.1 | |
* License: GNU General Public License | |
* License URI: http://www.wezo.com.br/blog/license/gnu-general-public-license/ | |
* | |
* Examples: | |
* <script type="text/javascript"> | |
* | |
* var bwg = browsing ; | |
* var params = new Object(); | |
* | |
* params.callback = function(){ | |
* console.log(["call function" , bwg.getParams() ]); | |
* }; | |
* | |
* bwg.init(params); | |
* | |
*/ | |
var browsing = { | |
separator : new String("&"), | |
separator_value : new String("="), | |
identifier_hash : new String("#"), | |
params : new Object(), | |
callback : null, | |
init : function(params) { | |
if(params !== undefined) { | |
if(params.separator !== undefined) { | |
this.separator = params.separator; | |
}; | |
if(params.separator_value !== undefined) { | |
this.separator_value = params.separator_value; | |
}; | |
if(params.callback !== undefined) { | |
this.callback = params.callback; | |
this.observer(); | |
}; | |
if(params.identifier_hash !== undefined) { | |
this.identifier_hash = params.identifier_hash; | |
}; | |
}; | |
}, | |
hash : function() { | |
return window.location.hash ; | |
}, | |
href : function() { | |
return window.location.href ; | |
}, | |
hasHash : function() { | |
if(this.hash() === "" || this.href().lastIndexOf(this.identifier_hash) <= 0) { | |
return false; | |
}else{ | |
return true; | |
} | |
}, | |
typeHash : function() { | |
return this.hasHash() === true ? typeof(this._hash) : null; | |
}, | |
cleanHash : function(str) { | |
return str.replace(this.identifier_hash,""); | |
}, | |
stringHash : function() { | |
if(this.identifier_hash === "#"){ | |
return this.cleanHash(this.hash()); | |
}else{ | |
return this.cleanHash(this.grabString()); | |
} | |
}, | |
grabString : function() { | |
var size_string = this.href().length; | |
var position_identifier_hash = this.href().lastIndexOf(this.identifier_hash); | |
if(size_string > 0) { | |
return this.href().substr(position_identifier_hash,size_string); | |
}; | |
return null; | |
}, | |
getParams : function() { | |
this.params = new Array(); | |
if(this.hasHash() === true) { | |
var array_params = this.stringHash().split(this.separator); | |
var array_k_v = []; | |
for ( var i = 0; i < array_params.length; i++) { | |
array_k_v = array_params[i].split(this.separator_value); | |
this.params[array_k_v[0]] = array_k_v[1]; | |
}; | |
}; | |
return this.params; | |
}, | |
observer : function() { | |
var callback = this.callback; | |
if (("onhashchange" in window) && !($.browser.msie)) { | |
window.onhashchange = function () { | |
return callback(); | |
}; | |
}else{ | |
var prevHash = this.hash(); | |
window.setInterval(function () { | |
if (this.hash() !== prevHash) { | |
storedHash = this.hash(); | |
return callback(); | |
} | |
}, 100); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment