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.
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.
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:
<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.
AddToFavorite("https://helloacm.com", "The Computing & Technology");
–EOF (The Ultimate Computing & Technology Blog) —
297 wordsLast 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?
