---
title: "兼容FireFox和IE8的设为首页和添加收藏"
author: "hsys"
date: "2010-02-01T18:40:04Z"
tags: []
categories: ["Web前端"]
slug: "firefox-ie8"
draft: false
lastmod: "2010-12-28T15:07:30Z"
id: 585255
url: "https://hsys.iteye.com/blog/585255"
---

设置首页和添加收藏是很常用的JS脚本，但是我以前经常用的那些脚本，在IE下是没有任何问题的，但是在FireFox下就不行了，今天上网查了一些资料，发现了可以兼容的脚本，经过修改测试，是可行的。


```html

<html>

<head>
<script>
function addCookie(){　 // 加入收藏夹
	if (document.all){
		window.external.addFavorite('http://www.caidao8.com', '财刀网');
	}else if (window.sidebar){
		window.sidebar.addPanel('财刀网', 'http://www.caidao8.com', "");
	}
}

function addCookie2(){
	try{
		window.external.AddFavorite('http://www.caidao8.com','财刀网'); 
	}catch(e){
		(window.sidebar)?window.sidebar.addPanel('财刀网','http://www.caidao8.com',''):alert('请使用按键 Ctrl+d，收藏财刀网'); 
	}
}

function setHomepage(){　 // 设置首页
	if (document.all){
		document.body.style.behavior = 'url(#default#homepage)';
		document.body.setHomePage('http://www.caidao8.com');
	}else if (window.sidebar){
		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', 'http://www.caidao8.com');
	}
} 
</script>
</head>
<body>
<a href="javascript:setHomepage();" >首页</a>

<a href="javascript:addCookie();">收藏"</a>
</body>
</html>

```