Home >Web Front-end >JS Tutorial >Native js writing is set as the homepage to be compatible with IE, Firefox and Google_javascript skills

Native js writing is set as the homepage to be compatible with IE, Firefox and Google_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:45:511396browse

Written in native js, compatible with IE, Firefox and Google.

Full code:

<script type="text/javascript">
//设为首页 www.jb51.net
function SetHome(obj,url){
  try{
    obj.style.behavior='url(#default#homepage)';
    obj.setHomePage(url);
  }catch(e){
    if(window.netscape){
     try{
       netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
     }catch(e){
       alert("抱歉,此操作被浏览器拒绝!
\n请在浏览器地址栏输入“about:config”并回车然后将[signed.applets.codebase_principal_support]设置为'true'");
     }
    }else{
    alert("抱歉,您所使用的浏览器无法完成此操作。
\n您需要手动将【"+url+"】设置为首页。");
    }
 }
}
 
//收藏本站 www.jb51.net
function AddFavorite(title, url) {
 try {
   window.external.addFavorite(url, title);
 }
catch (e) {
   try {
    window.sidebar.addPanel(title, url, "");
  }
   catch (e) {
     alert("抱歉,您所使用的浏览器无法完成此操作。
\n加入收藏失败,请进入新网站后使用Ctrl+D进行添加");
   }
 }
}
</script>
<a href="javascript:void(0);" onclick="SetHome(this,'http://www.jb51.net');">设为首页</a>
<div class="text text2"><a href="javascript:void(0);" onclick="AddFavorite('脚本之家','http://www.jb51.net')">收 藏 本 站</a>

You can refer to the following:

The function is as follows:

Copy code The code is as follows:

function setHome(obj,url){
try{
obj.style.behavior = 'url(#default#homepage)';
obj.setHomePage(url);
}catch(e){
if(window.netscape){
try{
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
}catch(e){
alert('Sorry, this operation was rejected by the browser! nPlease enter "about:config" in the browser address bar and press Enter. Then set the value of [signed.applets.codebase_principal_support] to true and double-click it. ');
}
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage',url);
}else{
alert('Sorry, the browser you are using cannot complete this operation. nYou need to manually set [' url '] as the homepage. ');
}
}
}

How to use:
Copy code The code is as follows:

fef50554eca1a427827adaa329da8122
49099650ebdc5f3125501fa170048923
93f0f5c25f18dab9d176bd4f6de5d30e
9d54027d814bba7a6c118810bbc6025d
b2386ffb911b14667cb8f0f91ea547a7Set as homepage6e916e0f7d1e588d4f442bf645aedb2f
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
7d1fb0999a3199c09c34a3a3ce1c5890Set as homepage5db79b134e9f6b82c0b36e0489ee08ed
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn