分享一段设为首页与加入收藏夹的代码,略显复杂,不过用来学习还是很不错的。
//加入收藏
function addFavorite() {
if(document.all) {
try { window.external.addFavorite(window.location.href, document.title);
} catch(e) {
alert("加入收藏失败,请使用Ctrl+D进行添加"); } }
else if(window.sidebar) {
window.sidebar.addPanel(document.title, window.location.href, ""); }
else { alert("加入收藏失败,请使用Ctrl+D进行添加"); } }
//设为首页
function setHomepage() {
if(document.all) {
document.body.style.behavior = 'url(#default#homepage)';
document.body.setHomePage(window.location.href); }
else if(window.sidebar) {
if(window.netscape) {
try { str = 'UniversalXPConnect';
netscape.security.PrivilegeManager.enablePrivilege(str); } catch(e) {
str = '该操作被浏览器拒绝,如果想启用该功能,';
str+='请在地址栏内输入 about:config,然后将项';
str+='signed.applets.codebase_principal_support值该为true';
alert(str); } }
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage', window.location.href); }
else { alert('您的浏览器不支持自动自动设置首页, 请使用浏览器菜单手动设置!');
} }