Skip to content

Instantly share code, notes, and snippets.

@JonMidhir
Last active August 29, 2015 14:03
Show Gist options
  • Save JonMidhir/62a3b004cae355f33992 to your computer and use it in GitHub Desktop.
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
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