JavaScript设置主页功能的代码

2019PHP高薪工程师学习路线图....>>>

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript设置“设为首页”和“收藏页面”(兼容IE和火狐浏览器)</title>
    <meta charset="utf-8">
    <script type="text/javascript">
        function setHomepage() {
            if (document.all) {
                /*IE*/
                document.body.style.behavior = 'url(#default#homepage)';
                document.body.setHomePage(window.location.href);
            } else if (window.sidebar) {
                /*FF*/
                if (window.netscape) {
                    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', window.location.href);
            } else {
                /*chrome或者其他*/
                alert("您的浏览器不支持自动自动设置首页,请使用浏览器菜单手动设置!");
            }
        }
    </script>
</head>
<body>
    <a onclick="setHomepage()" title="设为首页" href="javascript:void(0);">设为首页</a>
</body>
</html>