如果要兼容多浏览器就要判断用户的浏览器类型,如果是firefox还是ie6,7,8等,这样可以根据他们不能的区别进行判断再作操作。
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script language="网页特效" type="text/javascript">
function addfavorite(surl, stitle)
{
try
{
window.external.addfavorite(surl, stitle);
}
catch (e)
{
try
{
window.sidebar.addpanel(stitle, surl, "");
}
catch (e)
{
alert("加入收藏失败,请使用ctrl+d进行添加");
}
}
}
function sethome(obj,vrl){
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);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<a onclick="addfavorite(window.location,document.title)" style="cursor:pointer">加入收藏</a>
<a onclick="sethome(this,window.location)" style="cursor:pointer">设为首页</a>
</form>
</body>
</html>
简单代码
//添加到收藏夹
function addtofavorite()
{
if (document.all){
window.external.addfavorite(document.url,document.title);
}else if (window.sidebar){
window.sidebar.addpanel(document.title, document.url, "");
}
}
//设为首页
function sethomepage(){
if (document.all){
document.body.style.behavior='url(#default#homepage)';
document.body.sethomepage(document.url);
}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',document.url);
}
}
调用方法
<a href="#" onclick="sethomepage();">设为首页</a>
<a href="javascript:addtofavorite();" >添加收藏</a>