Dodać do ulubionych przeglądarek / zakładek z JavaScript ale dla wszystkich przeglądarek(moja nie działa w Chrome)?

Czy ktoś może pomóc, używam następujących do dodawania zakładki do IE i Firefoksa, ale jej nie działa w Chrome i nie dostaję mojego błędu MSG mówiąc "nie obsługiwane" albo..

Ktoś zna dobry skrypt do obsługi wszystkich przeglądarek lub przynajmniej wrócić i powiedzieć mi jego nie obsługiwane, mam dostęp do jQuery-może jest jakiś sposób, aby wykryć przeglądarkę

Obecnie używam tego i działa to dla IE i Firefoksa, ale nie chrome

if (window.sidebar) { // Mozilla Firefox
    window.sidebar.addPanel(name, url, "");
}
else if (window.external) { // IE
    window.external.AddFavorite(url, name);
}
else if (window.opera && window.print) {
    window.external.AddFavorite(url, name);
}
else {
    alert('not supported');
}
Author: Volker E., 2009-06-14

6 answers

Przykro mi, ale nie ma na to sposobu między przeglądarkami. Twój przykład FF jest również zepsuty: nie utworzy zwykłej zakładki, ale zakładkę ustawioną do otwarcia na pasku bocznym. Musisz użyć usługi zakładek , aby utworzyć zakładkę, ale to się nie powiedzie z powodu ograniczeń bezpieczeństwa.

 13
Author: Christoph,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-06-14 17:03:08

Po odkryciu-jak Edison! - kilka sposobów to nie działa, w końcu natknąłem się ta strona , która mówi dodawanie zakładek przez JS jest wyraźnie wyłączona w Chrome. Niestety nie wyjaśnia dlaczego.

Aktualizacja: zostałem poproszony o rozszerzenie tej odpowiedzi przez innego użytkownika SO...

Moje linki i przyciski do tej funkcji mają class="addbookmark" powiązane z nimi. Gdy User agent jest Chrome, używam jakiegoś jQuery, aby wyłączyć linki i wyjaśnić dlaczego:

<script type="text/javascript" src="/scripts/jquery-1.4.2.min.js"></script> 
<script type="text/javascript" src="/scripts/bookmark.js"></script> 
<script> 
    title='A Label for this Bookmark, ie title of this page'; // for example, not really generated this way... 

    $jQuery(document).ready(function(){ 
        // chrome does not permit addToFavorites() function by design 
        if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) { 
            $('.addbookmark').attr({ 
                title: 'This function is not available in Google Chrome. Click the star symbol at the end of the address-bar or hit Ctrl-D to create a bookmark.', 
                href: 'javascript:return false' 
            }) 
            .css({opacity: .25});       // dim the button/link 
        } 
    }); 
</script> 

A potem gdzie indziej na stronie:

 <td rowspan="2" class="noprint" style="width:24px;"> 
     <a class="addbookmark" title="Save a Bookmark for this page" 
        href="javascript:addToFavorites(location.href,title)"> 
        <img style="width:24px; height:24px; padding-top:2px;" src="/images/bookmark.gif"></a> 
 </td> 

... co bynajmniej nie jest idealne, ale wydaje się, że opcje są dość ograniczone.

Wersja jQuery nie jest ważna i od Ciebie zależy, czy chcesz mieć kopię lokalną, czy hot-link do wersji google. bookmark.js jest prawie dokładnie tak jak w kodzie OP:

$ cat /scripts/bookmark.js 
/* simple cross-browser script for adding a bookmark 
    source: http://stackoverflow.com/questions/992844/add-to-browser-favourites-bookmarks-from-javascript-but-for-all-browsers-mine-do 
*/ 
function addToFavorites(url, name) { 
    if (window.sidebar) { // Mozilla Firefox 
        window.sidebar.addPanel(name, url, ""); 
    } else if (window.external) { // IE 
        window.external.AddFavorite(url, name); 
    } else if (window.opera && window.print) { 
        window.external.AddFavorite(url, name); 
    } else { 
        alert("Sorry! Your browser doesn't appear to support this function."); 
    } 
} 
Mam nadzieję, że to się przyda.
 6
Author: RET,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-04-03 02:29:58

Właśnie testowałem ten skrypt w:

Win

  • IE 6.0, IE 7.0, IE 8.0
  • Firefox 2.0, Firefox 3.6.3
  • Safari 3.1.2, Safari 3.2.3
  • Opera 9.00
  • Google Chrome 8.0

Mac

  • Firefox 3.6.13
  • Safari 5.0.1
  • Opera 11.0
  • Google Chrome 8.0

    /*
    * Copyright 2010 by GlamThumbs Team.
    *
    * How To Use The Script:
    * add to your page this code between inside head tags
    * <script type="text/javascript" src="ATBookmarkApp.js"></script> 
    * add anchor with void href like this: 
    * <a href="javascript:void(0)" onClick="return BookmarkApp.addBookmark(this)">bookmark us</a> 
    * 
    */
    
    ATBookmarkApp = function () {
        var isIEmac = false; /*@cc_on @if(@_jscript&&!(@_win32||@_win16)&& 
    (@_jscript_version<5.5)) isIEmac=true; @end @*/
        var isMSIE = (-[1,]) ? false : true;
        var cjTitle = document.title;
        var cjHref = location.href;
    
        function hotKeys() {
            var ua = navigator.userAgent.toLowerCase();
            var str = '';
            var isWebkit = (ua.indexOf('webkit') != - 1);
            var isMac = (ua.indexOf('mac') != - 1);
    
            if (ua.indexOf('konqueror') != - 1) {
                str = 'CTRL + B'; // Konqueror
            } else if (window.home || isWebkit || isIEmac || isMac) {
                str = (isMac ? 'Command/Cmd' : 'CTRL') + ' + D'; // Netscape, Safari, iCab, IE5/Mac
            }
            return ((str) ? 'Press ' + str + ' to bookmark this page.' : str);
        }
    
        function isIE8() {
            var rv = -1;
            if (navigator.appName == 'Microsoft Internet Explorer') {
                var ua = navigator.userAgent;
                var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
                if (re.exec(ua) != null) {
                    rv = parseFloat(RegExp.$1);
                }
            }
            if (rv > - 1) {
                if (rv >= 8.0) {
                    return true;
                }
            }
            return false;
        }
    
        function addBookmark(a) {
            try {
                if (typeof a == "object" && a.tagName.toLowerCase() == "a") {
                    a.style.cursor = 'pointer';
                    if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) {
                        window.sidebar.addPanel(cjTitle, cjHref, ""); // Gecko
                        return false;   
                    } else if (isMSIE && typeof window.external == "object") {
                        if (isIE8()) {
                            window.external.AddToFavoritesBar(cjHref, cjTitle); // IE 8                    
                        } else {
                            window.external.AddFavorite(cjHref, cjTitle); // IE <=7
                        }
                        return false;
                    } else if (window.opera) {
                        a.href = cjHref;
                        a.title = cjTitle;
                        a.rel = 'sidebar'; // Opera 7+
                        return true;
                    } else {
                        alert(hotKeys());
                    }
                } else {
                    throw "Error occured.\r\nNote, only A tagname is allowed!";
                }
            } catch (err) {
                alert(err);
            }
    
        }
    
        return {
            addBookmark : addBookmark
        }
    }();
    
 5
Author: FFish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-16 07:51:06

Zawsze możesz ostrzec klienta, aby nacisnął ctr + D. Jest to uniwersalne we wszystkich przeglądarkach. Jest tandetny, ale równie przydatny dla klienta.

 4
Author: Arlo Carreon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-22 14:49:03

Nie mogłem uruchomić powyższego przykładu. W każdym razie odpowiedź na oryginalne pytanie "jego nie działa w CHROME i nie dostaję mój błąd msg mówiąc "nie obsługiwane" albo.."is due to the line

else if (window.external) { // IE 

Chrome rzeczywiście przechodzi ten test, a następnie obocznie nie dodaje zakładki. Zmieniłem ten wiersz na

else if(window.external && !window.chrome)  // IE

I teraz otrzymasz komunikat "Nie obsługiwane". Usunąłem tę wiadomość i wywołałem funkcję hotKeys (), aby uzyskać bardziej znaczący alert. Musiałem zrobić kilka zmiany, aby to zadziałało

function showHotKeys() 
{ 
var ua = navigator.userAgent.toLowerCase(); 
var str = ''; 
var isWebkit = (ua.indexOf('webkit') != - 1); 
var isMac = (ua.indexOf('mac') != - 1); 

if (ua.indexOf('konqueror') != - 1) { 
    str = 'CTRL + B'; // Konqueror 
} else if (window.home || isWebkit || isMac) { 
    str = (isMac ? 'Command/Cmd' : 'CTRL') + ' + D'; // Netscape, Safari, iCab
} 
return ((str) ? 'Press ' + str + ' to bookmark this page.' : str); 
} 
 2
Author: Tony K,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-10 04:16:14

Moje podejście z pomocą jQuery.
Testowane w IE 6-8, Fx 1-25, Operze 7-14. / Align = "left" /

CSS:

.no-js .link-bookmark {
   display: none;
}

JS:

/* ... Bookmark current page ... */
var $favLink = $('.link-bookmark');

if ( window.sidebar || 'AddFavorite' in window.external || window.opera ) {
    $favLink.show();
}

// add a 'rel' attrib if Op 7+ && Fx >= 23
if ( window.opera || window.sidebar ) {
    var $favLinkAttrRel = $favLink.attr('rel');
    if ( typeof $favLinkAttrRel !== "undefined" && $favLinkAttrRel !== false ) { // don't overwrite the rel attr if already set
        $favLink.attr('rel', 'sidebar');
    }
}

$favLink.click(function( event ) {
//event.preventDefault(); // prevent the anchor tag from sending the user off to the link
var url = this.href;
var $title = $('title').text();

// IE Favorite
if ( 'AddFavorite' in window.external ) {
    event.preventDefault();
    window.external.AddFavorite(url, $title);
}
// Fx <23 Bookmark, 'addPanel' not available from v23 on any more.
else if ( 'addPanel' in window.sidebar ) {
    event.preventDefault();
    window.sidebar.addPanel($title, url, '');
}
// Op 7+ && Fx >= 23
else if ( window.opera || window.sidebar ) {
    $favLink.attr('title', $title);
    return true; // do nothing - the rel="sidebar" should do the trick
}
// for Saf, Konq etc - browsers who do not support bookmarking scripts
else {
    event.preventDefault();
    alert('Your browser doesn\'t support the bookmark functionality,'
    + 'please add this page to your bookmarks manually.');
} 
});
 0
Author: Volker E.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-05 17:19:17