這段程式的設為首頁自動取得目前文件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();" />
</body>
</html>