Adding browser bookmarks using JavaScript

I have an ASP.NET webpage with a button in it. By clicking the button, the bookmark should be saved in the browser, and when the user clicks on the bookmark, it should search for http://google.com .

How can I make sure that it works with almost all standard browsers, or at least with IE, Mozilla Firefox, Opera and Google Chrome.

In another case, I create a second bookmark in the same way. But when the user clicks on the second bookmark, it should run part of the JavaScript code.

+4
source share
4 answers

I wrote this piece of code that works for IE, Firefox, and Opera (unfortunately, it does not work for Google Chrome).

function bookmark() { var title = 'Google'; var url = 'http://google.com'; if (document.all) // Check if the browser is Internet Explorer window.external.AddFavorite(url, title); else if (window.sidebar) //If the given browser is Mozilla Firefox window.sidebar.addPanel(title, url, ""); else if (window.opera && window.print) //If the given browser is Opera { var bookmark_element = document.createElement('a'); bookmark_element.setAttribute('href', url); bookmark_element.setAttribute('title', title); bookmark_element.setAttribute('rel', 'sidebar'); bookmark_element.click(); } } 
+1
source

Cannot be done for security reasons. This was previously possible using the proprietary IE command, but I think it ended in IE 7. Definitely not possible in others.

Related Discussion Mozilla Developer Central

+1
source

In Firefox, as far as I know, there is currently no function that adds a regular bookmark. In Firefox, only a sidebar bookmark can be created.

Here you can find a script that works for most browsers: http://labnol.blogspot.com/2006/01/add-to-favorites-ie-bookmark-firefox.html

As you can see on this line:

 window.sidebar.addPanel(title, url,""); 

it only adds a sidebar for Firefox, which is not very user friendly.

+1
source

For FireFox there is no need to set any javascript for bookmarking, only the anchor tag with title and rel = "sidebar" can perform this function

 <a href="http://www.google.com" title="Google" rel="sidebar">Bookmark This Page</a> 

I tested it on FF9

+1
source

Source: https://habr.com/ru/post/1311721/


All Articles