设为首页、加入收藏 兼容代码
一般写法:
<a href="javascript:window.external.AddFavorite(window.location.href,document.title)">加入收藏</a>
<a href="javascript:void()" onclick="this.style.behavior='url(#default#homepage)';this.setHomePage(location.href);">设为首页</a>
说明:这种写法是最常见的,兼容性不高
兼容写法:
<a onclick="AddFavorite(window.location,document.title)" href="javascript:void(0)">加入收藏</a>
<a onclick="SetHome(window.location)" href="javascript:void(0)">设为首页</a>
<script type="text/javascript" language="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(url)
{
if (document.all)
{
document.body.style.behavior='url(#default#homepage)';
document.body.setHomePage(url);
}
elseif (window.sidebar)
{
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',url);
}
}
</script>
说明:这种写法兼容性是比较高的
//成功一定有方法,失败一定有原因。