直接使用下面的代码,在IE有效果,火狐点击后没反应
<a href="javascript:;" onclick="javascript:window.external.AddFavorite('http://www.baidu.com','添加我为收藏')" >添加收藏</a>
使用下面的代码即可解决该问题:
<!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>