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
Release: 2016-05-16 16:45:51
Original
1394 people have browsed it

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>
Copy after login

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:





Set as homepage


Set as homepage


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template