Native JS writing, compatible with IE, Firefox and Google.
Complete code:
<script type= "Text/javascript" >//Set homepage 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 ("Universalxpconnec
T "); }catch (e) {alert ("Sorry, this operation was rejected by the browser!")
\ n \ nplease enter "About:config" in the browser address bar and return to the [Signed.applets.codebase_principal_support] setting to ' true '); }else{alert ("Sorry, the browser you are using cannot complete this operation.") \ n \ nyou need to manually set "+url+" as the home page.
");
}///Favorites This site Www.jb51.net function addfavorite (title, url) {try {window.external.addFavorite (URL, title);
catch (e) {try {window.sidebar.addPanel (title, URL, ""); catch (E) {alert ("Sorry, the browser you are using cannot complete this operation.")
\ n \ nyou failed to join the collection, please enter the new site after the use of ctrl+d to add "); }} </script> <a href= "javascript:void (0);" onclick= "sethome (This, ' http://www.jb51.net ');" > Set homepage </a> <div class= "text Text2" ><a href= "javascript:void (0);" onclick= "Addfavorite (' cloud-dwelling Community ', ' http :Www.jb51.net ') "> Favorites Site </a>
The following can be referred to:
The functions are as follows:
Copy Code code 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 is rejected by the browser! \ n \ nplease enter "About:config" in the browser address bar and return to \ n \ Then set the value of [Signed.applets.codebase_principal_support] to True and double-click. ');
}
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. \ n \ nyou need to manually set "' + URL + '" as the home page. ');
}
}
}
How to use:
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title> Homepage </title>
<body>
<a href= "javascript:;" onclick= "Sethome (this,window.location);" > Homepage </a>
</body>