Link This Page

I required a method of reliably providing a "Link To This Page" in a cross browser and cross OS environment. After searching around on the web, I decided nothing quite fit and so created my own solution. This code should reliably work in Mozilla, IE, and Opera browsers, but should also fail gracefully in any environment where it errors out or cannot find and appropriate "hook".

The functions look like this:

function createBookmarkLink(url, title) {
  try {
    if (window.sidebar) { // Mozilla Firefox Bookmark
      return window.sidebar.addPanel(title, url, "");
    } else if( window.external ) { // IE Favorite
      return window.external.AddFavorite( url, title); 
    } else if(window.opera && window.print) { // Opera Hotlist
      return true; 
    }
  } catch(err) {
    // do nothing
  }
  alert('Could not create a bookmark on your client.\n\nTo return to ' + title + ' you must manually create a bookmark to: \n' + url + '.');
}
function writeBookmarkLink(url, title) {
  try {
    if (window.sidebar) { // Mozilla Firefox Bookmark
      return document.write('Create a Bookmark to ' + title + ''); 
    } else if (window.external) { // IE Favorite
      return document.write('Add ' + title + ' to your Favorites'); 
    } else if (window.opera && window.print) { // Opera Hotlist
      return document.write('Create a Bookmark to ' + title + '');
    } 
  } catch(err) {
    // do nothing
  }
  return document.write('To return to ' + title + ' create a bookmark to: ' + url + '');
}

... and you would call it with something like this: