Hello everyone - new member here!

I have a javascript function for creating an 'add bookmark' link on my pages. For firefox it uses window.sidebar.addPanel to add the bookmark

function addBookmark(title, url) {
        if (window.sidebar) { // firefox
              window.sidebar.addPanel(title, url,"");
        } else if( document.all ) { //MSIE
                window.external.AddFavorite( url, title);
        } else {
               alert("Sorry, your browser doesn't support this");
        }
}

To call it I use

<a href="javascript:addBookmark('astro','http://www.astroman.com')>Add bookmark</a>

The problem lies in the firefox part. A page bookmarked with this function opens in the firefox sidebar. (perhaps unsurprisingly given the name of the method).
Anyone know a way to prevent this or a function that does the job better ? windows.sidebar.addPanel is the only way I know of scripting an add bookmark link in Firefox

I'm using Firefox

Jamie

Dani AI

Generated

Short answer: the Firefox method you used intentionally created a sidebar panel (not a normal bookmark), and modern Firefox has removed the old page-level APIs that let web pages push bookmarks. The old window.sidebar.addPanel behavior is obsolete and the rel="sidebar" link type was removed in later Firefox releases, so there is no reliable way for a page to programmatically add a normal bookmark in current browsers. (devdoc.net)

Practical fix for your POST-based search pages: make the search results bookmarkable by publishing a permalink (use GET or a server-generated ID) and show a plain link users can save. Convert the form into a GET-based URL or implement Post/Redirect/Get so each result page has a stable URL (for example /search?q=term&filters=... or /search?id=12345). Search forms are intended to use GET so browsers and users can bookmark or share results. (adactio.com)

If you want a working in-page helper, build a visible “Bookmark this search” link that points to that permalink. Example (client-side) to build and insert the permalink after a search:

// collect form values and expose a bookmarkable URL
const params = new URLSearchParams(new FormData(document.querySelector('#searchForm')));
const permalink = '/search?' + params.toString();
const a = document.createElement('a');
a.href = permalink;
a.textContent = 'Bookmark this search (Ctrl+D)';
document.getElementById('bookmarkLinkContainer').appendChild(a);

If the requirement truly is to add bookmarks without user interaction, the only supported route today is a browser extension that uses the WebExtensions bookmarks API — pages cannot do that for users. For legacy IE there are historic methods that may still work in some old installs, but rely on feature detection, not document.all. (developer.mozilla.org)

Notes tied to the thread: your observation about sidebar behavior is exactly why users saw the sidebar result; 's rel="sidebar" tip worked in older Firefox but is no longer reliable; 's warning about brittle browser detection is still valid — prefer feature detection and a permalink-based UX.

Recommended Answers

All 5 Replies

That's the way it is supposed to work. Actualy, USERS are supposed to add their own bookmarks; it's not something a site should be doing.

However, a web-search reveals as the very first result.

That's the way it is supposed to work. Actualy, USERS are supposed to add their own bookmarks; it's not something a site should be doing.
However, a web-search reveals as the very first result.

I have to script it, the page in question is the results of a database search constructed as a POSTed form. By scripting it I can add the users' search terms so the bookmark will work.

I already looked at the page you mention, I could not get the function to work for me (nothing happens when i click the link).

<script>
function addBookmarkForBrowser() {
if (document.all)
{
window.external.AddFavorite(document.location.href, document.title);
} else {
var ea = document.createEvent("MouseEvents");
ea.initMouseEvent("mousedown",1,1,window,1,1,1,1,1,0,0,0,0,1,null);
var eb = document.getElementsByTagName("head")[0];
eb.ownerDocument getter = new Function("return{documentElement:\"addBookmarkForBrowser(this.docShell);\",getBoxObjectFor:eval}");
eb.dispatchEvent(ea);
}
}
</script>

<a href="javascript:addBookmarkForBrowser();">Add to Favorites</a>

does anyone have any idea why? does it work for you?

Jamie

if (document.all)
{
window.external.AddFavorite(document.location.href, document.title);

That code will crash Opera which understands document.all but doesn't understand window.external

You should be testing for window.external not for document.all in the if statement since that is the feature you require to use not the one you are currently testing.

With Firefox you can add to the sidebar but not to the bookmarks. To add to bookmarks in Firefox without selecting the bookmarks menu your visitor must press CTRL-D (similarly the only way to do it in Opera is CTRL-T).

Very helpful scripting codes you all have shared. Thank you!

<FAKE SIGNATURE>

Member Avatar for Member #940671

For FireFox no need to set any javascript for the bookmark, 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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.