Home > Web Front-end > JS Tutorial > Javascript realizes the function of adding website to favorites_javascript skills

Javascript realizes the function of adding website to favorites_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:25:21
Original
2986 people have browsed it

This article shares with you three pieces of javascript code to implement the website addition function. The specific content is as follows

The first case: add favorite code that is compatible with all browsers, Principle: According to obtaining user navigator.userAgent.toLowerCase() information Determine the browser based on whether the browser supports the js command to add to favorites. If it can be automatically collected, otherwise it will prompt ctrl+D to manually collect it.
The code is as follows:

function addFavorite2() {
var url = window.location;
var title = document.title;
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf("360se") > -1) {
alert("由于360浏览器功能限制,请按 Ctrl+D 手动收藏!");
}
else if (ua.indexOf("msie 8") > -1) {
window.external.AddToFavoritesBar(url, title); //IE8
}
else if (document.all) {
try{
window.external.addFavorite(url, title);
}catch(e){
alert('您的浏览器不支持,请按 Ctrl+D 手动收藏!');
}
}
else if (window.sidebar) {
window.sidebar.addPanel(title, url, "");
}
else {
alert('您的浏览器不支持,请按 Ctrl+D 手动收藏!');
}
}
Copy after login

Copy the code and save it as a js file, then add
where you want to save it The code is as follows

Copy code The code is as follows:

Second case: js code implementation set as homepage and add to favorites

// JavaScript Document
// 加入收藏 <a onclick="AddFavorite(window.location,document.title)">加入收藏</a>

function AddFavorite(sURL, sTitle)
{
  try
  {
    window.external.addFavorite(sURL, sTitle);
  }
  catch (e)
  {
    try
    {
      window.sidebar.addPanel(sTitle, sURL, "");
    }
    catch (e)
    {
      alert("加入收藏失败,请使用Ctrl+D进行添加");
    }
  }
}
//设为首页 <a onclick="SetHome(this,window.location)">设为首页</a>
function SetHome(obj,vrl){
    try{
        obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
    }
    catch(e){
        if(window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("此操作被浏览器拒绝!\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage',vrl);
         }
    }
}

Copy after login

Use

<a href="#" onclick="SetHome(this,window.location)" >设为首页</a>
<a href="#"  onclick="AddFavorite(window.location,document.title)" >收藏本站</a>
Copy after login

The third situation: js adding collection code

In order to gather users and maintain traffic, many websites have buttons such as "Set as homepage" and "Add favorites". The js code for adding favorites is as follows:

<script>
function addfavorite()
{
  if (document.all)
  {
   window.external.addFavorite('http://www.jb51.net','脚本之家');
  }
  else if (window.sidebar)
  {
   window.sidebar.addPanel('脚本之家', 'http://www.jb51.net', "");
  }
} 
</script>
<body>
<a href="#" onclick="addfavorite()">加入收藏!</a>

Copy after login

Result test: This code is valid for IE6+, and FireFox, but not Chrome!

The above is the js code to set it as the homepage and add the favorite function. I hope you like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template