JS添加收藏,设置主页

来源:互联网 发布:nginx的内置变量 编辑:程序博客网 时间:2024/05/18 03:57
 
<script type="text/javascript">        function addFavorite() {            var thisURL = document.URL;            var favURL = "http://" + getHost(thisURL);            if (document.all) {                window.external.addFavorite(favURL, "网站名称");            } else {                window.sidebar.addPanel('网站名称', favURL, "");            }            return false;        }        function SetHome(obj) {            var vrl = "http://" + getHost(document.URL);            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);                }            }        }        var getHost = function (url) {            var host = "null";            if (typeof url == "undefined" || null == url)                url = window.location.href;            var regex = /.*\:\/\/([^\/]*).*/;            var match = url.match(regex);            if (typeof match != "undefined" && null != match)                host = match[1];            return host;        }    </script>

<a href="javascript:void(0)" class="white" onclick="addFavorite()">添加收藏</a><a href="javascript:void(0)" class="white" onclick="SetHome(this)">设为主页</a>

原创粉丝点击