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('<a href="javascript:createBookmarkLink(\'' + url + '\', \'' + title + '\')");">Create a Bookmark to <em>' + title + '</em></a>');
    } else if (window.external) { // IE Favorite
      return document.write('<a href="javascript:createBookmarkLink(\'' + url + '\', \'' + title + '\')");">Add <em>' + title + '</em> to your Favorites</a>');
    } else if (window.opera && window.print) { // Opera Hotlist
      return document.write('<a rel="sidebar" href="' + url + '" title="' + title + '">Create a Bookmark to <em>' + title + '</em></a>');
    }
  } catch(err) {
    // do nothing
  }
  return document.write('To return to ' + title + ' create a bookmark to: <a style="color: white;" href="' + url + '">' + url + '</a>');
}

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

<script>writeBookmarkLink('http://www.emanaton.com/', 'emanaton dot com');</script>
<noscript>To return to <em>emanaton dot com</em> create a bookmark to: <a href="http://www.emanaton.com/">http://www.emanaton.com/</a></noscript>

Comments

Post new comment

  • 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>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Use <fn>...</fn> to insert automatically numbered footnotes.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
5 + 7 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.