Last active
August 29, 2015 14:03
-
-
Save JonMidhir/62a3b004cae355f33992 to your computer and use it in GitHub Desktop.
Cross-browser, cross-platform JavaScript function to add the current page to the browser bookmarks
This file contains hidden or 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
addToBookmarks = -> | |
e.preventDefault() | |
title = document.title | |
url = document.location.href | |
# IE | |
if window.external?.AddFavourite | |
window.external.AddFavorite(url, title) | |
# Firefox <v24 | |
else if window.sidebar?.addPanel? | |
window.sidebar.addPanel(title, url, "") | |
# Firefox >v23 or Opera | |
else if window.sidebar? or window.opera? | |
ghostAnchor = document.createElement('a') | |
ghostAnchor.setAttribute('rel', 'sidebar') | |
ghostAnchor.setAttribute('title', title) | |
ghostAnchor.setAttribute('href', url) | |
ghostAnchor.click() | |
# Chome or other | |
else | |
userAgent = navigator.userAgent | |
userAgent = userAgent.toLowerCase() | |
isMac = /mac/.test(userAgent) | |
isChrome = /chrome/.test(userAgent) | |
message = "Press " | |
message += if isMac then "⌘" else "CTRL" | |
message += " + D" | |
message += " or click the star on the right of the browser address bar" if isChrome | |
message += " to bookmark this page." | |
alert message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment