How to Add To Favorite using Javascript?


We all know that in most modern browsers, you can use the short key Ctrl + D to add a website URL to the favorite. For example, in Microsoft Edge Browser, Ctrl+D yields the following dialog on the right top corner of the page.

add-to-favorite-ctrl-d How to Add To Favorite using Javascript? browsers javascript

add-to-favorite-ctrl-d

We can do this via the following Javascript function, which detects different browser and show a message if it is not supporrted in the browser.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var AddToFavorite = function(url, title) {
   var ua = navigator.userAgent.toLowerCase();
   if (ua.indexOf("msie 8") > -1) {
       external.AddToFavoritesBar(url, title, '');
   } else {
      try {
         window.external.addFavorite(url, title);
      } catch (e) {
         try {
             window.sidebar.addPanel(title, url, '');
         } catch (e) {
             alert("Please try shortcut Ctrl+D to add to favorite");
         }
     }
   }
   return false;
}
var AddToFavorite = function(url, title) {
   var ua = navigator.userAgent.toLowerCase();
   if (ua.indexOf("msie 8") > -1) {
       external.AddToFavoritesBar(url, title, '');
   } else {
      try {
         window.external.addFavorite(url, title);
      } catch (e) {
         try {
             window.sidebar.addPanel(title, url, '');
         } catch (e) {
             alert("Please try shortcut Ctrl+D to add to favorite");
         }
     }
   }
   return false;
}

Demo

Now, let’s do this via the demo, include the following JS:

1
<script async src='https://helloacm.com/js/addtofavorite.js'></script>
<script async src='https://helloacm.com/js/addtofavorite.js'></script>

Create a button and test it!

Add the following code to invoke the function at the button’s onclick event.

1
AddToFavorite("https://helloacm.com", "The Computing & Technology");
AddToFavorite("https://helloacm.com", "The Computing & Technology");

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
314 words
Last Post: Quick R Tutorial - How to Plot Sigmoid Function using R?
Next Post: R Programming Tutorial - How to Compute PI using Monte Carlo in R?

The Permanent URL is: How to Add To Favorite using Javascript?

Leave a Reply