首页 > 代码库 > 原生js编写设为首页兼容ie、火狐和谷歌

原生js编写设为首页兼容ie、火狐和谷歌

 1 //设为首页
 2 //<a href="javascript:void(0);" onclick="SetHome(this,window.location)">设为首页</a>
 3 function SetHome(obj, url) {
 4     try {
 5         obj.style.behavior = ‘url(#default#homepage)‘;
 6         obj.setHomePage(url);
 7     } catch (e) {
 8         if (window.netscape) {
 9             try {
10                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
11             } catch (e) {
12                 alert("抱歉,此操作被浏览器拒绝!\n\n请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]设置为‘true‘");
13             }
14         } else {
15             alert("抱歉,您所使用的浏览器无法完成此操作。\n\n您需要手动将【" + url + "】设置为首页。");
16         }
17     }
18 }
19 //收藏本站
20 //<a href="javascript:void(0);" onclick="AddFavorite(window.location,document.title)">加入收藏</a>
21 function AddFavorite(title, url) {
22     try {
23         window.external.addFavorite(url, title);
24     } catch (e) {
25         try {
26             window.sidebar.addPanel(title, url, "");
27         } catch (e) {
28             alert("抱歉,您所使用的浏览器无法完成此操作。\n\n加入收藏失败,请使用Ctrl+D进行添加");
29         }
30     }
31 }
32 //保存到桌面
33 //<a href="javascript:void(0);" onclick="toDesktop(location.href,document.title)">保存到桌面</a>
34 function toDesktop(sUrl, sName) {
35     try {
36         var WshShell = new ActiveXObject("WScript.Shell");
37         var oUrlLink = WshShell.CreateShortcut(WshShell.SpecialFolders("Desktop") + "\\" + sName + ".url");
38         oUrlLink.TargetPath = sUrl;
39         oUrlLink.Save();
40     } catch (e) {
41         alert("当前IE安全级别不允许操作!");
42     }
43 }

 

原生js编写设为首页兼容ie、火狐和谷歌