Link to home
Start Free TrialLog in
Avatar of PeterErhard
PeterErhard

asked on

Add Bookmark through html/javascript

I've got the following code, but it doesn't work (am using Firefox). When I click "Bookmark this page" nothing happens. Does anyone know what is wrong?

<span class="footer_margin"><a class="email_bookmark" href="javascript:addbookmark()">Bookmark this page</a></span>

<script language="javascript">

function addbookmark()
{
      var bookmarkurl=window.location;
      var bookmarktitle=document.title;

      if (document.all)
      window.external.AddFavorite(bookmarkurl,bookmarktitle)
}

</script>
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

IE only.
Here is the only thing I could find
<html>
<head>
<title>My cool bookmark</title>
 
<script>
function addBookmarkForBrowser() {
  if (document.all) {
    window.external.AddFavorite(window.location.href , document.title);
  } 
  else {
  /* Dunno where this code came from - even after fixing the .getter it still does not make any sense...
    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);
  */
  // this works but creates the bookmark in the sidebar !!!
    if (window.sidebar) window.sidebar.addPanel(document.title,window.location.href,"")
    else alert('Sorry - not in this browser');
  }
  
}
</script>
</head>
<body>
<a href="#" onClick="addBookmarkForBrowser(); return false">Add to Favorites</a>
</body>
</html>

Open in new window

PS: the sidebar stuff only works from a supported protocol like http or https. Not from file:
here is my version that i use - its also cross browser:


<a href="javascript:void(0);" title="Bookmark this Page" onclick="javascript:postToBrowser();return false;">Bookmark This Page</a>
 
function postToBrowser() {
	if(document.all) {
		window.external.AddFavorite(document.location.href,document.title);
	} else if(window.sidebar) {
		window.sidebar.addPanel(document.title, document.location.href,"");
	}
}

Open in new window

And this is different from mine how? Except I also alert in the case of other browsers

If I remove the comments and the debug alert, I have

<script>
function addBookmarkForBrowser() {
  if (document.all) {
    window.external.AddFavorite(window.location.href , document.title);
  } 
  else {
    if (window.sidebar) window.sidebar.addPanel(document.title,window.location.href,"")
    else alert('Sorry - not in this browser');
  }
}
</script>
 
<a href="#" onClick="addBookmarkForBrowser(); return false">Add to Favorites</a>

Open in new window

>>And this is different from mine how?

Its not - I read between the lines... its actually the same :-(  

I do apologise Michel :-)

Ellandrd
No problem :)
addBookmark script for:
//IE5+/Win, Firefox, Netscape 6+, Opera 7+, Safari, Konqueror 3, IE5/Mac, iCab 3

http://www.dynamicsitesolutions.com/javascript/add-bookmark-script/
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Only difference is it allows Opera to bookmark ..
Then I guess it truly is not doing the same thing you are since it takes Opera into consideration.

>>the rest gets a "Press some buttons to bookmark" where mine says "Sorry not in this browser"
Not all browser have the capability to add bookmark programatically. For those that cannot be done programatilly but support a key combination (like CTRL+B), the script "works" in the sense that it provides helpful information to the user so that the user ultimately knows what to do to bookmark the page.

As an end user, to me "Press 'CTRL + B' to bookmark" is more informative/useful than "Sorry not in this browser".

Also, those key combinations are not "made up". Last time I tested this on different version of opera, ie, netscape the Key combinations were dead on.

You may not like it because of its complexity, but its more "complete". At any rate, that's up to Peter to decide.
Avatar of PeterErhard
PeterErhard

ASKER

I prefer using hielo's option as it does seem more extensive.

One problem I'm having with it though is that when I use :

<script type="text/javascript" src="/js/add_bookmark.js">
</script>
<p id="addBookmarkContainer"></p>

It automatically creating another line afterwards which I don't like.

Do you know how I can control that, as on my page I want "Bookmark This Page | Email Us"  but it puts in a line after Bookmark This Page.

If it put Bookmark This Page in there manually its fine
>>Do you know how I can control that
You can use a <div> instead of a <p>

Another alternative is to add a css definition for addBookmarkContainer
#addBookmarkContainer{margin-bottom:0;}

You could even get rid of the "top" spacing as well:
#addBookmarkContainer{margin-bottom:0;margin-top:0;}
What I commented on was that it was CLAIMED it works in
//IE5+/Win, Firefox, Netscape 6+, Opera 7+, Safari, Konqueror 3, IE5/Mac, iCab 3
when in reality it works in IE and possibly Opera and alerts something for all other browsers.
I call it false advertising...

You can easily change "Sorry not in this browser"
to "In order to bookmark this site you need to do so manually through your browser."

So adding such a huge script for the enormous number of opera users out there who do not know how to bookmark a page feels like massive overkill.

I suggest you place this code where you want your span
<script language="javascript"><!--
if (document.all && window.external) {
document.write('<span class="footer_margin"><a class="email_bookmark" href="#"'+
' onClick="window.external.AddFavorite(window.location,document.title); return false">'+
'Bookmark this page</a></span>');
} //--></script>

Open in new window

>>You can easily change "Sorry not in this browser" to "In order to bookmark this site you need to do so manually through your browser."

I agree, but it is still not as useful as alerting the user of what the necessary key combinations to bookmark the link manually. Thus, this:
 "In order to bookmark this site you need to do so manually through your browser."

is still not as useful as:
"Press Ctrl + B to bookmark this site."
And there are so many people not knowing how to do that - especially on exotic browsers
Hi, All, If it's useful, I have found this site, and their little bookmark link works on IE and FF:
http://www.addthis.com