Native JS writing set homepage compatible with IE, Firefox and Google _javascript tips

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.