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:



Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre>
  • Lines and paragraphs break automatically.
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • Web page addresses and e-mail addresses turn into links automatically. (Better URL filter.)
  • Syntax highlight code surrounded by the {syntaxhighlighter OPTIONS}...{/syntaxhighlighter} tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.