Bookmark your Webpage?

Post on October 6th, 2008

I’m not a fan of creating a script that only works in one browser or differently in other browser(s). I had to create a link on a website for work that made a URL their home page. This annoying task proved difficult as there wasn’t much good information online and it seems only IE allows this. The Firefox browser will only allow you to make a web page a ‘favorite’.

So I found the code to make it work on both IE and Firefox (will not work on any other browser), as well as improving its ‘unobtrusiveness’. As you can see from the code, I only display the link if the browser is either Internet Explorer or Firefox otherwise the link text is blank.

The ‘Browser.Detect’ object is from a script from the JavaScript guru himself, PPK which detects what browser the visitor is using. Browser detection isn’t used as much today as it once was but it is still needed on some occasions.

Internet Explorer

Firefox

function browserBoomkark(id) {
 
var link = document.getElementById(id);
 
switch (BrowserDetect.browser) {
 
     case 'Explorer':
 
          link.innerHTML = 'Set to Home Page';	
          link.style.cursor = 'pointer';
          link.onclick = function () {
               this.style.behavior='url(#default#homepage)';
               this.setHomePage('URL');
          }
 
      break;
 
      case 'Firefox': // only supports making page a favorite
 
           link.innerHTML = 'Set to Favorite';
           link.style.cursor = 'pointer';
           link.onclick = function () {
 
                window.sidebar.addPanel('label', 'URL', "");
           }
 
           break;
 
      default:
 
           // nothing is displayed for other browsers
      }
}

Test the page yourself to test it out and download the code (.zip).

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.