2011-04-29 39 views

Trả lời

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> 

Mã này được lấy từ Developersnippets!

/e:

Chrome không hỗ trợ hành động như vậy, vì mức độ bảo mật có thể bị phá vỡ.

+0

Tôi làm cách nào để làm việc trong Chrome? Trong Chrome, thông báo cảnh báo thậm chí không hiển thị ... – 585connor

+1

Để tránh phát hiện lỗi trong Chrome, bạn nên sử dụng 'else if (window.external && window.external.AddFavorite)' vì 'window.external' ist được định nghĩa trong Chrome, nhưng không phải 'window.external.AddFavorite'. –

1

Hãy thử điều này:

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

Tôi nghĩ plugin jquery Bookmark là thứ bạn đang tìm kiếm. jBrowserBookmark cho phép bạn thêm chức năng vào một trang web cho phép một trang được thêm vào danh sách boookmark của trình duyệt. Tính năng này được hỗ trợ bởi trình duyệt Internet Explorer, Firefox, Opera và Konqueror.Bạn có thể nhận được nó here

7

Vì Chrome không hỗ trợ hành động như vậy, nên có thể kiểm tra trước nếu trình duyệt sử dụng Chrome và nếu có cảnh báo người dùng rằng chức năng đánh dấu không được hỗ trợ. Sau đó, đối với các trường hợp khác, tập lệnh được cung cấp trên DevelopersSnippets hoạt động tốt.

Ví dụ:

$("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; 
    } 
    }); 
Các vấn đề liên quan