使用時直接調(diào)用函數(shù),不需設(shè)定網(wǎng)站標(biāo)題等
<span onclick ='addFav()'> 收藏本站 </span>
<span onclick ='setHomepage()'> 設(shè)為首頁 </span>
<script type='text/javascript'>
function addFav(){ // 加入收藏夾
if (document.all) {
window.external.addFavorite(window.location.href, document.title);
} else if (window.sidebar) {
window.sidebar.addPanel(document.title, window.location.href, "");
}
}
function setHomepage(){ // 設(shè)置首頁
if (document.all) {
document.body.style.behavior = 'url(#default#homepage)';
document.body.setHomePage(window.location.href);
} else if (window.sidebar) {
if(window.netscape) {
try {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
}
catch (e) {
alert('此操作被瀏覽器拒絕!\n請在瀏覽器地址欄輸入“about:config”并回車\n然后將 [signed.applets.codebase_principal_support]的值設(shè)置為’true’,雙擊即可。');
}
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage', window.location.href);
}
}
}
</script>
if (window.sidebar)是判斷FF的一種方法,就像判斷IE用if (document.all)一樣