Created
July 19, 2011 19:29
-
-
Save Fordi/1093484 to your computer and use it in GitHub Desktop.
Shortcut icon manager
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
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