Skip to content

Instantly share code, notes, and snippets.

@devi
Created January 28, 2011 16:43
Show Gist options
  • Save devi/800524 to your computer and use it in GitHub Desktop.
Save devi/800524 to your computer and use it in GitHub Desktop.
hashbang
// jquery hashbang(#!)
function hasbang(){
$('a').each(function(){
var o = $(this), url = o.attr('href'), p = url.match('^https?://');
var h1 = location.hostname, h2 = h1.replace(/^www\./i,'');
if (url == '#') {
o.attr('href','javascript:void(0);');
return;
}
if (url.match('^#!') || url.match('^javascript:'))
return;
if (!p || url.match(h1) || url.match(h2)){
if (p){
url = url.substr(p[0].length);
if(url.indexOf('/') != -1)
url = url.substr(url.indexOf('/'));
else
url = '';
}
if (url.charAt(0) == '#')
return;
if (url.charAt(0) == '/')
url = url.substr(1);
if (!url){
o.attr('href',location.protocol + '//'+ h1);
return;
}
url = '#!'+url.replace(/\#\!/g,'');
o.attr('href',url);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment