How to add to Favorites/Bookmark for all browser in javascript [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-06 13:59:04

问题


I have some query in Add to favorites/Bookmarked web page using java script.I just go through in this Link and get this source code. It's been working perfectly for the last month. Yet now it will not work in any browser. Here is my code :

function CreateBookmarkLink(){
        var title = document.title;
        var url = document.location.href;

        if(window.sidebar){
            /* Mozilla Firefox Bookmark */
            window.sidebar.addPanel(title, url, "");
        }else if(window.external){
            /* IE Favorite */
            window.external.AddFavorite(url, title);
        }else if(window.opera && window.print) {
            /* Opera Hotlist */
            alert("Press Control + D to bookmark");
            return true;
        }else{
            /* Other */
            alert("Press Control + D to bookmark");
        }
 <a href="javascript:CreateBookmarkLink();">Add to Favorites/Bookmark</a>

It doesn't work in any browser any more and just displays:

TypeError: window.sidebar.addPanel is not a function
> window.sidebar.addPanel(title, url, "");

Any ideas how to solve it? I also need to add favorites in chrome browser. Any other idea to create bookmark for my website.


回答1:


Due to security reasons it is not possible to add a bookmark in Google Chrome using Javascript.

Alternatively, you could output a message for using the shortcut:

    $('#bookmarkme').click(function(){
        alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
    }); 

Since window.sidebar.addPanel is deprecated and not a standard implementation (https://developer.mozilla.org/en-US/docs/Web/APIWwindow.sidebar), you may use the API to add bookmarks when creating an add-on (https://developer.mozilla.org/en-US/docs/Code_snippets/Bookmarks).

Nevertheless it should not be a high burden for users to add their favorite website as bookmark inside the browser.



来源:https://stackoverflow.com/questions/18457572/how-to-add-to-favorites-bookmark-for-all-browser-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!