• 兼容火狐和IE的加入收藏夹/设为首页代码

    日期:2009-11-12 | 分类:互联网

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://ericneo.blogbus.com/logs/51136594.html

    <!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>IE 火狐FF 设为首页 加入收藏 完全适应代码</title>
    </head>
    <script type="text/javascript" language="javascript">
    <!-- 加入收藏代码 Start -->
    function AddFavorite(sURL, sTitle) {
    try {
    window.external.addFavorite(sURL, sTitle);
    } catch (e) {
    try {
    window.sidebar.addPanel(sTitle, sURL, "");
    } catch (e) {
    alert("加入收藏失败,请手动添加.");
    }
    }
    }
    <!-- 加入收藏代码 End -->
    <!-- 设为首页代码 Start -->
    function setHomepage(pageURL) {
    if (document.all) {
    document.body.style.behavior='url(#default#homepage)';
    document.body.setHomePage(pageURL);
    }
    else if (window.sidebar) {
    if(window.netscape) {
    try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    }
    catch (e) {
    alert( "该操作被浏览器拒绝,如果想启用该功能,请在地址栏内输入 about:config,然后将项signed.applets.codebase_principal_support 值该为true" );
    }
    }
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
    prefs.setCharPref('browser.startup.homepage',pageURL);
    }
    }
    <!-- 设为首页代码 End -->
    </script>

    <body>
    <a href="javascript:setHomepage('http://www.baidu.com');">设为首页</a>
    <a href="#" onclick="javascript:AddFavorite('http://www.baidu.com', '百度一下,你就知道');return false">加入收藏</a>
    </body>
    </html>


    收藏到:Del.icio.us