【问题标题】:Bookmark on click using jQuery使用jQuery点击书签
【发布时间】:2018-08-22 05:51:03
【问题描述】:

当点击特定按钮时,有没有办法将当前页面保存为书签(通过 jQuery 或其他方式)?

【问题讨论】:

  • 在您自己的浏览器中?或一些社交媒体网络?

标签: javascript jquery


【解决方案1】:
<script language="javascript" type="text/javascript">
$(document).ready(function(){
  $("a.jQueryBookmark").click(function(e){
    e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
    var bookmarkUrl = this.href;
    var bookmarkTitle = this.title;

    if (window.sidebar) { // For Mozilla Firefox Bookmark
        window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
    } else if( window.external || document.all) { // For IE Favorite
        window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
    } else if(window.opera) { // For Opera Browsers
        $("a.jQueryBookmark").attr("href",bookmarkUrl);
        $("a.jQueryBookmark").attr("title",bookmarkTitle);
        $("a.jQueryBookmark").attr("rel","sidebar");
    } else { // for other browsers which does not support
         alert('Your browser does not support this bookmark action');
         return false;
    }
  });
});
</script>

此代码取自Developersnippets!

/e:

Chrome 不支持此类操作,因为可能会破坏安全级别。

【讨论】:

  • 我怎样才能让它在 Chrome 中工作?在 Chrome 中,警报消息甚至不显示...
  • 为了防止在 Chrome 中抛出错误,你应该使用 else if(window.external &amp;&amp; window.external.AddFavorite),因为 window.external 是在 Chrome 中定义的,而不是 window.external.AddFavorite
【解决方案2】:

由于 Chrome 不支持此类操作,解决方案可能是首先检查正在使用的浏览器是否为 Chrome,如果是,则提醒用户不支持书签功能。然后对于其他情况,DevelopersSnippets 上提供的脚本可以正常工作。

例子:

   $("a.bookmark").click(function(e){
    e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
    var bookmarkUrl = this.href;
    var bookmarkTitle = this.title;
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) { 
            alert("This function is not available in Google Chrome. Click the star symbol at the end of the address-bar or hit Ctrl-D (Command+D for Macs) to create a bookmark.");      
    }else if (window.sidebar) { // For Mozilla Firefox Bookmark
        window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
    } else if( window.external || document.all) { // For IE Favorite
        window.external.AddFavorite( bookmarkUrl, bookmarkTitle);          
    } else if(window.opera) { // For Opera Browsers
        $("a.bookmark").attr("href",bookmarkUrl);
        $("a.bookmark").attr("title",bookmarkTitle);
        $("a.bookmark").attr("rel","sidebar");
    } else { // for other browsers which does not support
         alert('Your browser does not support this bookmark action');
         return false;
    }
  });

【讨论】:

    【解决方案3】:

    试试这个:

    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if(window.opera && window.print){ // opera
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel','sidebar');
        elem.click();
    } 
    else if(document.all)// ie
        window.external.AddFavorite(url, title);
    }
    

    【讨论】:

      【解决方案4】:

      我认为 jquery 书签插件是您正在寻找的。 jBrowserBookmark 允许您向站点添加功能,从而允许将页面添加到浏览器书签列表中。 Internet Explorer、Firefox、Opera 和 Konqueror 浏览器均支持此功能。您可以获取它here

      【讨论】:

      • 将旧链接替换为另一个链接,不确定是否为同一作者,但它是搜索此名称时的第一个链接之一。该插件只是对其他答案中方法的封装。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多