7

I'm using the following JavaScript code:

<script language="JavaScript1.2" type="text/javascript">
 function CreateBookmarkLink(title, url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url,"");
    } else if( window.external ) {
        window.external.AddFavorite( url, title); }
    else if(window.opera && window.print) {
        return true; }
 }
</script>

This will create a bookmark for Firefox and IE. But the link for Firefox will show up in the sidepanel of the browser, instead of being displayed in the main screen. I personally find this very annoying and am looking for a better solution. It is of course possible to edit the bookmark manually to have it not show up in the side panel, but that requires extra steps. I just want to be able to have people bookmark a page (that has a lot of GET information in the URL which is used to build a certain scheme) the easy way.

I'm afraid that it might not be possible to have Firefox present the page in the main screen at all (as Googling this subject resulted in practically nothing worth using), but I might have missed something. If anyone has an idea if this is possible, or if there's a workaround, I'd love to hear about it.

Nickolay
  • 31,095
  • 13
  • 107
  • 185
Michiel
  • 352
  • 6
  • 11
  • By the way, `window.opera && window.print` seems to target Opera > v6.
    And the `language` attribute is deprecated and shouldn't be declared: http://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.2.2
    – Volker E. Dec 05 '13 at 15:50

4 Answers4

7

For Firefox no need to set any JavaScript for the bookmark an page by script, only an anchor tag with title and rel="sidebar" can do this functionality

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

I have tested it on FF9 and its working fine.

When you click on the link, Firefox will open an dialog box New Bookmark and if you wish to not load this bookmark on side bar then un-check Load this bookmark in the sidebar from dialog box.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Atul Kushwah
  • 83
  • 1
  • 4
  • +1 thanks, this saved me.. but a more general way can be found here: http://stackoverflow.com/questions/17747578/uncaught-typeerror-object-htmldivelement-has-no-method-addpanel – reikyoushin Jul 19 '13 at 18:02
4

I think that's the only solution for Firefox... I have a better function for that action, it works even for Opera and shows a message for other "unsupported" browsers.

<script type="text/javascript">
function addBookmark(url,name){
    if(window.sidebar && window.sidebar.addPanel) {
        window.sidebar.addPanel(name,url,''); //obsolete from FF 23.
} else if(window.opera && window.print) { 
        var e=document.createElement('a');
        e.setAttribute('href',url);
        e.setAttribute('title',name);
        e.setAttribute('rel','sidebar');
        e.click();
} else if(window.external) {
        try {
            window.external.AddFavorite(url,name);
        }
        catch(e){}
}
else
        alert("To add our website to your bookmarks use CTRL+D on Windows and Linux and Command+D on the Mac.");
}
</script>
Community
  • 1
  • 1
iBobo
  • 714
  • 8
  • 13
  • you are correct. there is no way to add a page to bookmarks in firefox without it opening in a side panel. – Jan Hančič Sep 20 '08 at 12:11
  • this code will break if you have an element with id="sidebar" as mentioned here: http://stackoverflow.com/questions/17747578/uncaught-typeerror-object-htmldivelement-has-no-method-addpanel – reikyoushin Jul 19 '13 at 18:00
0

You have a special case for

if (window.sidebar) 

and then a branch for 'else' - wouldn't firefox land in the first branch and hence only add the panel?

Per Hornshøj-Schierbeck
  • 15,097
  • 21
  • 80
  • 101
0

Hojou,

It seems that is the only way to add a bookmark for Firefox. So FF needs to land in the first branch to have anything happening at all. I Googled some more but I'm really getting the idea this is impossible to properly address in FF...

Michiel
  • 352
  • 6
  • 11