如何在javascript中为所有浏览器添加到收藏夹/书签 [英] How to add to Favorites/Bookmark for all browser in javascript

查看:492
本文介绍了如何在javascript中为所有浏览器添加到收藏夹/书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用java脚本添加到收藏夹/已添加书签的网页中有一些查询。我只是通过这个链接并获取此源代码。它在上个月一直运作良好。但现在它不适用于任何浏览器。这是我的代码:

  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);
}否则if(window.opera&& window.print){
/ * Opera Hotlist * /
alert(按Control + D to bookmark);
返回true;
} else {
/ * Other * /
alert(按Control + D加入书签);
}
< a href =javascript:CreateBookmarkLink();>添加到收藏夹/书签< / a>

它不再适用于任何浏览器,只显示:


TypeError:window.sidebar.addPanel不是函数

> window.sidebar.addPanel(title,url,) ;


任何想法如何解决?我还需要在Chrome浏览器中添加收藏夹。为我的网站创建书签的任何其他想法。

解决方案

出于安全原因,无法在Google Chrome中添加书签使用Javascript。



或者,您可以输出使用快捷方式的消息:

  $('#bookmarkme')。点击(function(){
alert('按'+(navigator.userAgent.toLowerCase()。indexOf('mac')!= - 1?'命令/ Cmd':'CTRL')+'+ D为此页面添加书签。');
});

由于 window.sidebar.addPanel 已弃用而非标准实施( https://developer.mozilla.org/en -US / docs / Web / APIWwindow.sidebar ),您可以在创建附加组件时使用API​​添加书签( https://developer.mozilla.org/en-US/docs/Code_snippets/Bookmarks )。



<但是,对于用户来说,在浏览器中添加自己喜爱的网站作为书签不应该是一个很大的负担。


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.

解决方案

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.

这篇关于如何在javascript中为所有浏览器添加到收藏夹/书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆