views:

79

answers:

1

This code

function bookmark(url, title) {  
    var a = document.createElement('a');
    a.setAttribute('href',url);
    a.setAttribute('title',title);
    a.setAttribute('rel','sidebar');
    a.click();
}

doesn't work in Opera 9 and 10. It just takes you to the page I set in url param.

Is there code compatible with Opera?

A: 

Just try this crossbrowser code

function addBookmark(title, url, obj){
if (!url) url = location.href;
if (!title) title = document.title;
if ((typeof window.sidebar == "object") && 
  (typeof window.sidebar.addPanel == "function")) 
 window.sidebar.addPanel (title, url, "");//Gecko
else if (document.all) 
 window.external.AddFavorite(url, title); //IE4+
else if (window.opera && document.createElement) {
 obj.setAttribute('rel','sidebar');
 obj.setAttribute('href',url);
 obj.setAttribute('title',title);
}
else {
 alert("Your browser does not support bookmarks
  Press ctrl+D (ctrl+T for old Opera);");
 return false; //IF Opera 6
}
return true;
}

Usage: <a href="#" onclick="addBookmark('google','http://google.com', this);"> Bookmark us! </a>

or

var a = document.getElementsByTagName('a')[0]; if(a.addEventListener) { a.addEventListener("click", function() { addBookmark('google','google.com', a); return false; }, false); } else if (a.attachEvent) { a.attachEvent("onclick", function() { addBookmark('google','google.com', a); return false; });}

It works for me (Opera 10.00)

antyrat
I use the same code, but it doesn't work for Opera
Yrgl
http://deer.org.ua/samples/bookmark/index.html Check this example. It works at my Opera v10.00
antyrat
yep, it works, but it doesn't work if use<pre>var a = document.getElementsByTagName('a')[0];a.onclick = function() { addBookmark('google','http://google.com', this); return false;}</pre>How can I fix it?
Yrgl
Try this code var a = document.getElementsByTagName('a')[0]; a.onclick = function() { addBookmark('google','google.com', a); return false; }
antyrat
it doesn't work neither
Yrgl
Try <code>var a = document.getElementsByTagName('a')[0]; if(a.addEventListener) { a.addEventListener("click", function() { addBookmark('google','google.com', a); return false; }, false);} else if (a.attachEvent) { a.attachEvent("onclick", function() { addBookmark('google','google.com', a); return false; });}</code>It forks fine at my Opera. If you want I send you worked example.
antyrat