tags:

views:

117

answers:

4

I want to make a bookmark of my webpage by using javascript code. I want a javascript code to make a bookmark that should be opened in a new window.

I am using firefox.

+2  A: 

Use this function:

function bookmark_us(url, title){
if (window.sidebar) // firefox
    window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
    var elem = document.createElement('a');
    elem.setAttribute('href',url);
    elem.setAttribute('title',title);
    elem.setAttribute('rel','sidebar');
    elem.click();
}
else if(document.all)// ie
    window.external.AddFavorite(url, title);
}
Andreas Grech
Grant Wagner
+1  A: 

In Firefox there is currently no way to do this (and there are bugs filed in bugzilla tracking this defect)

By "no way to do this" I mean that you can use the function Dreas gave but you will be limited to adding a bookmark that will default to the sidebar. The end user will have to manually un-check the "open in sidebar" option.

scunliffe
A: 

Ask on one of the Mozilla user groups (probably mozilla.dev.tech.xul or mozilla.dev.tech.xpcom). You could conceivably create the right XPCOM object to access the browser's bookmark manager -- this requires enabling privileges (the user will have to explicitly approve this). Not portable to other browsers (obviously) and perhaps not a great way of doing this.

Most websites I've seen that facilitate bookmarking (Jesse Ruderman's bookmarklet page is a great example) just ask the user to drag the appropriate bookmark to the bookmark folder of their choice, and this seems so much more straightforward/simpler than trying to automatically add something to the bookmarks.

Jason S
A: 
function bookmark_us(url, title){
    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if(window.opera && window.print){ // opera
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel','sidebar');
        elem.click();
    } else if (document.all) // ie
        window.external.AddFavorite(url, title);
}

When used with:

<a href="#" onclick="bookmark_us('http://www.yahoo.com/', 'Yahoo!');return false;">Bookmark Us</a>

The above function no longer works as desired in Opera 9.x. It simply navigates to www.yahoo.com.


To make a link that pops up the Add Bookmark dialog in Opera 9.x, use the following:

function bookmark_us(url, title) {
    if (window.sidebar && window.sidebar.addPanel) // firefox
        window.sidebar.addPanel(title, url, "");
    else if (window.external && 'undefined' != typeof window.external.AddFavorite) // ie
        window.external.AddFavorite(url, title);
}

<a href="http://www.yahoo.com/" rel="sidebar" title="Yahoo!" onclick="bookmark_us(this.href, this.title);return false;">Bookmark Us</a>
Grant Wagner