DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   JavaScript / DHTML / AJAX (http://www.daniweb.com/forums/forum117.html)
-   -   Scripting an add bookmark link in Firefox (http://www.daniweb.com/forums/thread45103.html)

kincaid May 9th, 2006 7:44 am
Scripting an add bookmark link in Firefox
 
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 1.5.0.3

Jamie

tgreer May 9th, 2006 9:55 am
Re: Scripting an add bookmark link in Firefox
 
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 this page as the very first result.

kincaid May 10th, 2006 6:03 am
Re: Scripting an add bookmark link in Firefox
 
Quote:

Originally Posted by tgreer
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 this page 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

felgall May 11th, 2006 8:10 pm
Re: Scripting an add bookmark link in Firefox
 
Quote:

Originally Posted by kincaid
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).


All times are GMT -4. The time now is 9:39 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC