Adding Bookmarking Functionality


In Internet Explorer browsers, you can add a feature to add a bookmark (shortcut to your site) to the user's "Favorites" menu by clicking a link. A box will pop up asking where you want this bookmark to be. The IE-specific Javascript syntax for this is:

window.external.AddFavorite( PageURL , BookmarkText ); In other browsers however, this method does not work; but usually pressing Ctrl+D adds a bookmark to your bookmarks list. So, it would be nice to add a little code to tell the user to add the bookmark in another way if they aren't using IE. Since you have to add more code, it looks "cleaner" to create a function to handle bookmarking instead. We'll call this function setBookmark(). The function can now be designed to accept the URL and bookmark text parameters to make the function modular. Here is the full function which you can copy and paste directly into your page:

function setBookmark(url,str){
if(str=='')str=url;
if (document.all)window.external.AddFavorite(url,str);
else alert('Press CTRL and D to add a bookmark to:\n"'+url+'".');
}


The two parameters for the setBookmark( url , str ) function are: This function tests if the user is using IE. If so, it will use the add-to-Favorites method. If not, it prompts you to add the bookmark by pressing Ctrl+D while displaying the URL.

Here are some examples on how to use this function:

These functions can be called like any other javascript function; For more information on this and similar topics, head over to the Microsoft MSDN Library website.