Add to browser favorites/bookmarks from JavaScript but for all browsers (mine doesn't work in Chrome)?

前端 未结 6 1770
时光取名叫无心
时光取名叫无心 2020-11-28 09:38

Can anyone help, I am using the following for adding a bookmark to IE and Firefox but its not working in Chrome and I don\'t get my error msg saying \"not supported\" either

6条回答
  •  再見小時候
    2020-11-28 10:07

    After discovering - like Edison! - a bunch of ways this doesn't work, I eventually came across this page that says adding bookmarks via JS is explicitly disabled in Chrome. Unfortunately it does not explain why.

    Update: I was asked to expand this answer by another SO user...

    My links and buttons for this function all have a class="addbookmark" associated with them. When the user agent is Chrome, I use some jQuery to disable the links and explain why:

     
     
     
    

    And then elsewhere on the page:

      
          
             
      
    

    ... which is by no means perfect, but it seems one's options are fairly limited.

    The version of jQuery isn't important, and it's up to you whether you want a local copy or hot-link to the google version. bookmark.js is pretty much exactly as per the OP's code:

    $ cat /scripts/bookmark.js 
    /* simple cross-browser script for adding a bookmark 
        source: http://stackoverflow.com/questions/992844/add-to-browser-favourites-bookmarks-from-javascript-but-for-all-browsers-mine-do 
    */ 
    function addToFavorites(url, name) { 
        if (window.sidebar) { // Mozilla Firefox 
            window.sidebar.addPanel(name, url, ""); 
        } else if (window.external) { // IE 
            window.external.AddFavorite(url, name); 
        } else if (window.opera && window.print) { 
            window.external.AddFavorite(url, name); 
        } else { 
            alert("Sorry! Your browser doesn't appear to support this function."); 
        } 
    } 
    

    Hope that's useful.

提交回复
热议问题