javascript设置首页,加入收藏

<a href="javascript:;" id="setHomePage" class="toolsbar" onclick="setHomePage(this);">设为主页</a>
<a href="javascript:;" class="toolsbar" id="addFavorite" onclick="addFavorite(this);">加入收藏</a>
        //设置首页
        function setHomePage(obj) {
            var aUrls = document.URL.split("/");
            var vDomainName = "http://" + aUrls[2] + "/";
            try {//IE
                obj.style.behavior = "url(#default#homepage)";
                obj.setHomePage(vDomainName);
            } catch (e) {//other
                if (window.netscape) {//Firefox
                    try {
                        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                    }
                    catch (e) {
                        alert("此操作被浏览器拒绝!
请在浏览器地址栏输入“about:config”并回车
然后将[signed.applets.codebase_principal_support]设置为'true'");
                    }
                    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                    prefs.setCharPref('browser.startup.homepage', vDomainName);
                }
            }
            if (window.netscape) alert("设置首页成功!");
            return false; //阻止a标签继续执行
        }
        //加入收藏函数
        function addFavorite(obj) {
            var aUrls = document.URL.split("/");
            var vDomainName = "http://" + aUrls[2] + "/";
            var description = obj.title;
            try {//IE
                window.external.AddFavorite(vDomainName, description);
            } catch (e) {//Firefox
                window.sidebar.addPanel(description, vDomainName, "");
            }
            return false; //阻止a标签继续执行
        }
function funBackTop() {
            var elem = $('.backtop'), h = window.screen.height, time = null, srctop = 0;
            this.init = function () {
                elem.bind('click', function () {
                    $('#container').scrollTop(0);
                })

                time = setInterval(function () {
                    srctop = $('#container').scrollTop();
                    if (srctop > h) {
                        elem.show();
                    }
                    else {
                        elem.hide();
                    }
                }, 500)
            }
        }
设为首页 和 收藏本站js代码 兼容IE,chrome,ff
 
//设为首页
function SetHome(obj,url){
    try{
        obj.style.behavior='url(#default#homepage)';
        obj.setHomePage(url);
    }catch(e){
        if(window.netscape){
            try{
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }catch(e){
                alert("抱歉,此操作被浏览器拒绝!

请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]设置为'true'");
            }
        }else{
            alert("抱歉,您所使用的浏览器无法完成此操作。

您需要手动将【"+url+"】设置为首页。");
        }
    }
}
 
//收藏本站
function AddFavorite(title, url) {
    try {
        window.external.addFavorite(url, title);
    }
    catch (e) {
        try {
            window.sidebar.addPanel(title, url, "");
        }
        catch (e) {
            alert("抱歉,您所使用的浏览器无法完成此操作。

加入收藏失败,请使用Ctrl+D进行添加");
        }
    }
}
 
<a href="javascript:void(0);" onclick="SetHome(this,document.domain);">设为首页</a>
<a href="javascript:void(0);" onclick="AddFavorite('我的网站',location.href)">收藏本站</a>
原文地址:https://www.cnblogs.com/yeminglong/p/3466918.html