Add Bookmarks and Favorites

Related Articles

I was surprised to find that none of the major script outlets, didn't have a cross browser add bookmark script. So here is one that will allow you to add bookmarks to Internet Explorer, Netscape 6+ and Opera 7.

You would think in this day and age, we would have one mechanism to add bookmarks that works in all browsers. Unfortunately each of these browsers has different methods. So let us take a peek at the script to better understand what is occurring with each statement.

1. function addBookmark(title,url) {
2. if (window.sidebar) {
3. window.sidebar.addPanel(title, url,"");
4. } else if( document.all ) {
5. window.external.AddFavorite( url, title);
6. } else if( window.opera && window.print ) {
7. return true;

Line 1: Creates a function named addBookmark() that uses two arguments. The title argument will be used to define the title of the page from an event handler. Similarly, we use the url argument to define the current location of the document.

Line 2: Employs a conditional statement to separate out Netscape 6 and above from other browsers.

Line 3: In Netscape 7 we need to add our page to the sidebar by using window.sidebar.addPanel();

Line 4: Creates a conditional statement for Internet Explorer that separates this browser from others.

Line 5: Internet Explorer uses the window.external.AddFavorite() command to list a bookmark.

Line 6: Uses a conditional statement to separate Opera from other browsers.

Line 7: In Opera we can return the document as true and that will add a link to the Opera HotList / Sidebar panel. Press F4 if you haven't seen this feature in Opera as yet.

Line 8-9: Closes the function.

Now all we need to do is call the function from an event handler and pass the arguments for the title and url like so;

<a href="#" onmousedown="addBookmark('eddies page','')">Add bookmark</a>

Quiet a simple and effective script.

Publication Date: Friday 5th September, 2003
Author: Eddie Traversa View profile

Related Articles