JS设为首页、加入收藏代码 兼容IE,FireFox

来源:岁月联盟 编辑:zhuzhu 时间:2009-05-06
这段程序的设为首页自动获取当前文档URL的域名,加入收藏的描述取自当前文档的标题。

代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>设为首页--加入收藏</title>
<script type="text/javascript">
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) {//ff
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',vDomainName);
}
}
if(window.netscape)alert("ff");
}
function addFavorite(){
var aUrls=document.URL.split("/");
var vDomainName="http://"+aUrls[2]+"/";
var description=document.title;
try{//IE
window.external.AddFavorite(vDomainName,description);
}catch(e){//FF
window.sidebar.addPanel(description,vDomainName,"");
}
}

</script>
</head>

<body>
<input type="button" value="设为首页" onclick="setHomePage(this);" /> <input type="button" value="加入收藏" onclick="addFavorite();" />
新客网www.xker.com
</body>
</html>