2011-04-29 69 views
11

當某個按鈕被點擊時,是否有方法將當前頁面保存爲書籤(通過jQuery或其他方式)?使用jQuery點擊書籤

+0

?或到一些社交媒體網絡? – wegginho 2011-04-29 07:05:48

+0

在瀏覽器中。 – 585connor 2011-04-29 07:12:07

回答

9
<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不支持這樣的行動,因爲安全級別可能被打破。

+0

我如何才能在Chrome中使用它?在Chrome中,警報消息甚至不顯示... – 585connor 2011-04-29 07:17:19

+1

爲了防止在Chrome中拋出錯誤,您應該使用'else if(window.external && window.external.AddFavorite)',因爲'window.external'在Chrome中定義了,但不是'window.external.AddFavorite'。 – 2013-05-14 08:45:14

1

試試這個:

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); 
} 
1

我覺得jquery書籤插件是你在找什麼。 jBrowserBookmark允許你添加功能到一個網站,允許一個頁面被添加到瀏覽器boookmark列表。此功能由Internet Explorer,Firefox,Opera和Konqueror瀏覽器支持。您可以得到它here

7

由於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; 
    } 
    }); 
在自己的瀏覽器