Skip to content

Instantly share code, notes, and snippets.

@Fordi
Created July 19, 2011 19:29
Show Gist options
  • Save Fordi/1093484 to your computer and use it in GitHub Desktop.
Save Fordi/1093484 to your computer and use it in GitHub Desktop.
Shortcut icon manager
jQuery.icon = (function ($) {
var types = {
ico: 'image/x-icon',
gif: 'image/gif',
png: 'image/png',
jpg: 'image/jpeg',
jpeg: 'image/jpeg'
},
_icon,
_stack=[],
$icon = function () {
if (_icon) return _icon;
_icon = $('link[rel="shortcut icon"]');
if (_icon.length) return $icon;
_icon = $('<link>')
.attr('rel', 'shortcut icon')
.appendTo('head:first');
return _icon;
},
_ = {
get: function () {
return $icon().attr('href');
},
set: function (url) {
$icon()
.attr('href', url)
.attr('type', url?(types[url.replace(/^.*\.(ico|gif|jpe?g|png)$/i, '$1').toLowerCase()]):'')
.remove();
if (url) $icon().appendTo('head:first');
},
push: function (url) {
_stack.push(_.get());
_.set(url);
},
pop: function () {
if (_stack.length==0) return;
var now = _.get(),
top =_stack.pop();
_.set(top);
return now;
}
};
return _;
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment