problem creating bookmark link in Opera, Chrome, and Safari
Hello,
I've searched high and low for some decent code that will allow a visitor to a website to bookmark the site through a hyperlink. The bookmarking is done through a javascript function that looks like this:
Code:
function OnBookmarkButtonClick (e)
{
if (window.sidebar)
{ // Mozilla Firefox Bookmark
window.sidebar.addPanel("TimeSheet", window.document.location,"");
}
else if (window.external)
{ // IE Favorite
window.external.AddFavorite( window.document.location, "TimeSheet");
}
}
I need to add some detection for Opera, Safari, and Chrome. Please note that it must be feature detection, not browser detection. But as I said, I've searched high and low for this, and couldn't find anything.
function CreateBookmarkLink(alink) {
var title = "GOOGLE";
var url = "http://www.google.com";
if (window.sidebar) { // Mozilla Firefox Bookmark
window.sidebar.addPanel(title,url,"");
} else if( window.external ) { // IE Favorite
window.external.AddFavorite(url,title); }
else if(window.opera && window.print) { // Opera Hotlist
alink.href=url;
return true; }
}
//
if (window.external) {
document.write('<a href = "javascript:CreateBookmarkLink(this)");">Add to Favorites</a>');
} else if (window.sidebar) {
document.write('<a href ="javascript:CreateBookmarkLink(this)");">Bookmark Page</a>');
} else if (window.opera && window.print) {
document.write('<a rel="sidebar" href = "javascript:CreateBookmarkLink(this)");">Add Bookmark</a>');
}
For Chrome and Safari there is no JavaScript solution. Simply those browsers do not permit the Bookmark handling via JavaScript. You may, eventually, detect Chrome and Safari and launch an alert, something like:
Code:
else if(window.chrome){
alert('Press ctrl+D to bookmark (Command+D for macs) after you click Ok');
}
Is testing for window.opera considered browser detection? This is what I'm trying to avoid.
Sir, no detection of the browser type is 100% reliable, because browsers can be spoofed. The best way to detect a browser is detecting a specific method, but I don't think Opera has such of method
Bookmarks