Bookmark link that works for ALL 3 main browsers

Discussion in 'JavaScript' started by joshm, Mar 27, 2006.

  1. #1
    I have been looking for some javascript that allows a bookmark link to work on all 3 browsers: IE, Firefox, Opera. I am using a button for the bookmark function. I know there is some js code that works with a <a href> link but am curious as to whether I can make this work on all browsers using a button.

    I have the below code in a javascript file named /bm.js:

    function bookmark(url,title){
      if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
        window.external.AddFavorite(url,title);
      } else if (navigator.appName == "Netscape") {
        window.sidebar.addPanel(title,url,"");
      } else {
        alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
      }
    }
    Code (markup):
    In the <head> tag of my page I have:
    <script type="text/javascript" src="/js/bm.js"></script>
    Code (markup):
    In the body of my page I have:
    <form action="#">
    <input type="button" value="Bookmark Us" onclick="bookmark('http://wwwmydomainname.com','MyDomainName')">
    </form>
    Code (markup):
    Now, it works fine in IE and Firefox but upon clicking on the bookmark button in Opera, it does not even display the alert message as expected. I am using Opera V8.53. It doesn't like this script at all. Can there be a way for the bookmark button to function properly in Opera and allow people to add to favorites? Any help much appreciated!
     
    joshm, Mar 27, 2006 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is your Opera identifying itself as Opera or as IE?
     
    exam, Mar 31, 2006 IP
  3. joshm

    joshm Peon

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    How can I find out this information?
     
    joshm, Apr 1, 2006 IP
  4. brian394

    brian394 Well-Known Member

    Messages:
    226
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    108
    #4
    Go into your Tools menu and then select Quick Preferences to see what Opera is identifying itself as.
    You can also press F12 at any time to bring up this menu.

    Speaking of bookmark scripts, here is a "Bookmark for all browsers" script that I wrote a while ago. Seems to work correctly in IE, FireFox, and Opera. Let me know what you think...

    Put this in your Javascript...
    
    function bookmarkPage(t) {
     if (window.external) { window.external.AddFavorite(location.href, document.title); }
     else if (window.sidebar) { window.sidebar.addPanel(document.title, location.href, ""); }
     else if (window.opera) { if (t.rel == "sidebar") { t.href = location.href; t.title = document.title; } else { alert('To bookmark this page, click "OK" below    \nand then press CTRL-T on your keyboard.'); } }
     else { alert('To bookmark this page, click "OK" below    \nand then press CTRL-D on your keyboard.\n(Macintosh users press Command-D.)'); }
    }
    
    Code (markup):
    And put this in your (X)HTML...
    
    <a href="javascript:void(0)" onclick="bookmarkPage(this)" rel="sidebar">Bookmark This Site</a>
    
    Code (markup):
    Remember to add the rel="sidebar" attribute to your bookmark link for it to work in Opera.
     
    brian394, Jun 16, 2006 IP
    eXe likes this.