Just how 'unobtrusive' are you thinking? Personally, I think the general idea of bookmarking scripts are pointless. I know of three ways to do it, so a potentially non-functioning fourth alternative (depending on my browser and its configuration) seems unnecessary and inferior.
That said, as I see it there are three potential scenarios.
- The fully-functional. The browser supports at least one of the implemented ways of adding a bookmark, has sufficient DOM support (if necessary), and have client-side scripting support enabled.
- The partial failure. The browser has client-side scripting support enabled, but that support fails to provide one of the other features mentioned above.
- Complete failure. The browser either doesn't implement a script engine, or that support has been disabled at the user's request.
It's clear what should happen in the first scenario, but what do you plan for the second? Presenting something that claims to work but doesn't (and showing a dialogue box telling the user to "use [their] browser menu" isn't 'working') fails to be either professional or unobtrusive.
There are, perhaps, two solutions:
- Include the text, "please bookmark this site" somewhere in the document and then, given adequate support, transform "bookmark this site" into something that at least resembles a link and will perform the advertised operation.
- Given adequate support, insert both text and behaviour into the document at some point.
The difference, in case it's not clear, is whether the request to add a bookmark is always present, or only if bookmarking is possible using the script. The former will be more easier for a third party to use, especially if they are not that familiar with the DOM.
Code:
.link {
background: transparent;
color: #0000ff;
cursor: pointer;
cursor: hand;
text-decoration: underline;
}
Code:
this.onload = function() {
var listener = ('object' == typeof external)
? function() {external.AddFavorite(url, title); return false;}
: ('object' == typeof sidebar) && ('function' == typeof sidebar.addPanel)
? function() {sidebar.addPanel(title, url, ''); return false;}
: null,
title = document.title,
url = document.URL,
bookmark;
if(listener && document.getElementById
&& (bookmark = document.getElementById('bookmark'))
&& ('string' == typeof title)
&& ('string' == typeof url))
{
bookmark.className = 'link';
bookmark.onclick = listener;
}
bookmark = listener
= null;
};
HTML Code:
<p>Please <span id="bookmark">bookmark this site</span>.</p>
if (!document.getElementById || !document.all) /* object detection to prevent errors */
Feature detection is the correct approach to take, wherein a more or less one-to-one test-to-feature examination of the host enviroment is used to determine whether a particular operation can be executed. Trying to infer support based on the presence of unrelated objects, properties or methods is typically doomed to failure.
myURL = window.location.href; /* gets url of page to bookmark */
myTitle = document.title; /* gets title of page */
Why are these global?
if (document.all) /* is this MSIE? */
Browser detection, by virtually any method, is also doomed to failure. Not only will IE type-convert that expression to true, but so will Opera, Omniweb, Konqueror, Safari, NetFront, iCab, and IceBrowser. There may even be others.
Mike
Bookmarks