任何人都可以提供帮助,我使用以下内容为IE和Firefox添加书签,但它无法在Chrome中运行,我也没有收到我的错误信息说“不支持”..
任何人都知道支持所有浏览器的好脚本或至少回来告诉我它不受支持,我可以访问jQuery - 也许有一些方法来检测浏览器
我目前正在使用它,它适用于IE和Firefox但不适用于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');
}
答案 0 :(得分:13)
很抱歉,但没有跨浏览器的方式来执行此操作。你的FF示例也被破坏了:它不会创建常规书签,而是设置为在侧边栏中打开的书签。您必须use the bookmark-service才能创建实际的书签,但由于安全限制,这会失败。
答案 1 :(得分:6)
发现后 - 像爱迪生一样! - 这种方法不起作用,我最终遇到this page,表示在Chrome中明确禁用通过JS添加书签。不幸的是,它没有解释原因。
更新:我被要求由另一个SO用户扩展此答案...
此功能的我的链接和按钮都与class="addbookmark"
相关联。当用户代理是Chrome时,我使用一些jQuery来禁用链接并解释原因:
<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>
然后在页面的其他地方:
<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>
......这绝不是完美的,但似乎一个选项相当有限。
jQuery的版本并不重要,您需要本地副本或google version的热门链接取决于您。 bookmark.js
几乎完全符合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.");
}
}
希望这很有用。
答案 2 :(得分:5)
我刚刚测试了这个脚本:
运
的Mac
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
}
}();
答案 3 :(得分:4)
您始终可以提醒客户按ctr + D.这在所有浏览器中都是通用的。它很俗气,但同样对客户有用。
答案 4 :(得分:2)
我无法让上面的例子工作。无论如何,原始问题的答案是“它在CHROME中不起作用,而且我没有得到我的错误消息说”不支持“或者......是由于该行
else if (window.external) { // IE
chrome实际上通过了这个测试,然后很难添加书签。我将此行更改为
else if(window.external && !window.chrome) // IE
现在您收到了“不支持”消息。我实际上删除了这条消息,并调用函数hotKeys()来获得更有意义的警报。我不得不做一些改变才能让它发挥作用
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);
}
答案 5 :(得分:0)
我在jQuery的帮助下的方法 在IE 6-8,Fx 1-25,Opera 7-14中测试。在Chrome,Saf。中优雅地降级。
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.');
}
});