網站設為首頁和加入收藏JavaScript程式碼
阿新 • • 發佈:2019-02-13
// JavaScript Document // 加入收藏 <a onclick="AddFavorite(window.location,document.title)">加入收藏</a> function AddFavorite(sURL, sTitle) { try { window.external.addFavorite(sURL, sTitle); } catch (e) { try { window.sidebar.addPanel(sTitle, sURL, ""); } catch(e) { alert("加入收藏失敗,請使用Ctrl+D進行新增"); } } } //設為首頁 <a onclick="SetHome(this,window.location)">設為首頁</a> 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); } } }
使用
<a href="#" onclick="SetHome(this,window.location)" >設為首頁</a>
<a href="#" onclick="AddFavorite(window.location,document.title)" >收藏本站</a>