日期:2014-05-16  浏览次数:20341 次

关于js 添加到收藏夹代码,IE7,IE8报错
在IE6,firefox下都可以,可在IE7,IE8下报错找不到元素

function add_to_favorite(url,name)
{

if (document.all) 

window.external.addFavorite(url,name); 

else if (window.sidebar) 

window.sidebar.addPanel(name,url); 
}
}

哪位大哥帮忙解决一下。

------解决方案--------------------
<a onclick="window.external.AddFavorite(location.href, document.title);" title=" xxx" style="cursor:hand">收藏本页</a> </p>
------解决方案--------------------
不同浏览器会有兼容问题,以下是我写的代码,在IE和FIREFOX测试通过。


JScript code


// JavaScript Document
 //添加到收藏夹  
 // 调用方法:
 // <a href="Javascript:void(0)" onclick="AddToFavorite()" title="如果您喜欢本网站,请点击此处<br>加入您的收藏夹中">加入收藏</a>
 function AddToFavorite()  
 {  
     if (document.all){  
        window.external.addFavorite(document.URL,document.title);  
     }else if (window.sidebar){  
        window.sidebar.addPanel(document.title, document.URL, "");  
     }  
 }  
   
 
/**
* 设为首页 调用方法:
* <a href="javascript:void(0);" target="_self" onclick="setHomepage(this)">设为首页</a>
*/
function setHomepage(obj){
    vrl = document.URL;
    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);
        }
    }
}

------解决方案--------------------
很好,收藏了!!

~~~~~~~~~~~~~~~~
------解决方案--------------------
亲爱的同学们,我也遇到这个问题了。上面小朋友写的代码,我试了在ie8下面报“找不到元素”的错误。
微软的官方网站:http://msdn.microsoft.com/en-us/library/cc197035(VS.85).aspx
给了:AddToFavoritesBar 这个方法,但是同样不可以。报错同上。
那么是我的IE8有问题,还是IE8的bug。有没有哪位在IE8上,实验是成功的?
请把下面的代码copy到htm文件中测试下,谢谢!
<script language='javascript'>
function savePage(){
if(window.external){ 
window.external.AddToFavoritesBar('http://www.csdn.net', 'csdn','slice');
}
}
</script>
<input type=button onclick='savePage()' value='加入收藏夹'>
------解决方案--------------------
在ie8下面报“找不到元素”