📜 ⬆️ ⬇️

Bookmarks and Javascript

Faced with the task to create a link (or button) “Add to bookmarks” on the site, several solutions were found to solve this problem. And everything seems to be good, but everyone doesn’t please me.



And so it was selected 2 options
')
The first

function AddToFavorites(title, url) {
if (window.sidebar) { // Mozilla Firefox Bookmark
window.sidebar.addPanel(title, url,"");
return false;
}
else if( window.external ) { // IE Favorite
window.external.AddFavorite( url, title);
return false;
}
else if(window.opera && window.print) { // Opera Hotlist
var elem = document.createElement('a');
elem.setAttribute('href',url);
elem.setAttribute('title',title);
elem.setAttribute('rel','sidebar');
elem.click();
return false;
}
}


And the second

And the jFav1.0 plugin under jQuery.

To load the jQuery library with a plugin, I didn’t really like it, for the sake of one small link.
and the first code did not want to work out in the Opera.

I had to turn on the brain:
in IE there is a window.external.AddFavorite( url, title); - everything is simple
in the FF window.sidebar.addPanel(title, url,""); - also easy
in Opera it was added using the jFav1.0 plugin
having picked in the code of this plug-in, I realized (thanks to the creators of jFav1.0) that for the Opera there is no need for any script, a rather simple link add this page to bookmark
add this page to bookmark
with attribute rel = "sidebar" .
“Microformats” - I thought, that's what's the use of you.

FF favorite, didn’t let me down either, by clicking on such a link, opens the “Add to bookmarks” window.

Safari and Chrome were not limited, quietly crossed the page indicated in the link.


For them, I did not find a good solution, I had to stop at a banal alert('Pls, press Ctrl + D or CMD + D for MAC, \n to add this page to your bookmarks.');

The result is the following code:
// JS
function bookmark(a){
if (window.sidebar){ // firefox
return false;
}
else if(window.opera && window.print){ // opera
return false;
}
else if(document.all){ // ie
window.external.AddFavorite(a.href2 || a.href, a.title);
if(!a.href2){
a.href2 = a.href;
a.href="#";
}
} else {
alert('Pls, press Ctrl + D or CMD + D for MAC, \n to add this page to your bookmarks.');
a.href=+"#";
return false;
}
}
//HTML

<a href="http://yoursite/" rel="sidebar" onclick="bookmark(this)" title="My JS Bookmarks" > add this page to bookmark </a>


Working example

The question of Safari and Chrome,
Can someone have an elegant solution for them?

The code was tested under Windows PX - FF3.5.4, Opera 10.01. IE6-8, Safary 3.2.1, Chrome 3.0.195.27

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


All Articles