Monday, June 03, 2013

Adding BookMarks Through Javascript

Whenever we like a page on web, we can bookmark it for future consult. We need to hit CTRL+D keystrokes to bring the bookmark dialogue and after we click on "Ok" button, the current URL is saved in browser bookmark list. In IE, it gets listed in Favorites listings. Here we would be bringing the Save Bookmark dialogue box on screen though JavaScript code. Check it out ...

<script>
function bookmark_me(sURL,sTitle)
{
 // IE
 if ( window.external && document.all)
 {
  window.external.AddFavorite (sURL,sTitle);
 }
 // Now for firefox
 else if (window.sidebar) 
 {
   window.sidebar.addPanel(sTitle,sURL,'');
 }
  // Opera
 else if(window.opera)
 {
   var p = document.createElement("a");      
   p.setAttribute("title", sTitle);
   p.setAttribute('rel','sidebar');
   p.setAttribute('href','http://google.com');
   p.setAttribute('onclick','return false;');
   p.click();
   return false;
 }
}
</script>

<a rel='sidebar' href="javascript:;" onclick="bookmark_me('http://google.com','Google')">BookMark Me !!</a>


My version of firefox (v.21) has access to window.external, hence we had to use document.all check for IE. Chrome does not allow adding favorites/bookmarks though JavaScript. The "rel='sidebar'" in the above link is a must for it to work in Opera (my version is 12.15).

No comments: