为网站添加 Javascript 而非 jquery 书签 [英] Bookmark Javascript not jquery for website

查看:26
本文介绍了为网站添加 Javascript 而非 jquery 书签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下书签 Javascript.

I have the following bookmark Javascript.

function bookmark(title, url) {
   if(document.all) { // ie
       window.external.AddFavorite(url, title);
   }
   else 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(); // this.title=document.title;
    }
}

和 HTML

<a href="javascript:bookmark('title of the page', 'http://www.domain.com');" class="bookmark" >

问题是它只能在 Internet Explorer 中运行.不适用于 firefox、opera、chrome.另外我听说firefox已经弃用了window.sidebar.addPanel函数,有什么办法可以解决所有这些问题?请不要使用 JQUERY.

And the problem is that is working only in Internet Explorer. Is not working in firefox, opera, chrome. Also i heard that firefox have deprecated the function window.sidebar.addPanel, is there any way to fix all of this? PLEASE NO JQUERY.

推荐答案

以下是如何使用来自 如何添加添加到收藏夹"?我网站上的按钮或链接?没有 jQuery 事件绑定.

Here's how to use the answer from How do I add an "Add to Favorites" button or link on my website? without the jQuery event binding.

function bookmark(title, href) {
    if (window.sidebar && window.sidebar.addPanel) { // Mozilla Firefox Bookmark
        window.sidebar.addPanel(title,href,'');
    } else if(window.external && ('AddFavorite' in window.external)) { // IE Favorite
        window.external.AddFavorite(href,title); 
    } else if(window.opera && window.print) { // Opera Hotlist
        this.title=title;
        return true;
    } else { // webkit - safari/chrome
        alert('Press ' + (navigator.userAgent.toLowerCase().indexOf('mac') != - 1 ? 'Command/Cmd' : 'CTRL') + ' + D to bookmark this page.');
    }
}

这篇关于为网站添加 Javascript 而非 jquery 书签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆