Set homepage and bookmark JS Code (multi-browser support) _javascript tips

Source: Internet
Author: User

The latest code to support IE, Firefox, and chrome messages:

<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> 

First share two set the first page of the JS code (does not conform to the standard does not talk about compatibility):

Two ways to set the current page as your browser's home page, one is to set the page you are browsing as the homepage, and the other is to set the designated Web site as the first page, as to what you use, see your needs, each has a suitable occasion.

<ptml> <pead> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <title> set home page JS code </title> </pead> <body> <p> method One: <span onclick= ' var strhref=window.location.href;this.style.behavior= ' url (#default #homepage) '; This.sethomepage (' http:// Www.jb51.net '); "> This homepage </span></p> <p> method Two: Set home page </p> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


The following code compatibility is good and standard, but IE7 is not able to support the collection, but it is already a better code
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "Content=" Tex t/html; charset=gb2312 "/> <title> homepage with Add Favorites code compatible multi-browser" </title> <script language= "JavaScript" > function Addbookmark (Title,url) {if (Window.sidebar) {window.sidebar.addPanel (title, URL, ""); else if (document.all) {window.external.AddFavorite (URL, title); else if (Window.opera && window.print) {return true; } function Sethome (URL) {if (document.all) {document.body.style.behavior= ' url (#default #homepage) '; Document.body.setHomePage (URL); }else if (Window.sidebar) {if (window.netscape) {try{Netscape.security.PrivilegeManager.enablePrivilege ("Universalx Pconnect "); }catch (e) {alert ("This action is rejected by the browser, if you want to enable this feature, enter About:config in the Address bar, and then the item Signed.applets.codebase_principal_supPort value should be true "); } if (Window.confirm () are you sure you want to set the "+url+" as the homepage? ") ==1 {var prefs = components.classes[' @mozilla. Org/preferences-service;1 '].getservice ( Components.interfaces.nsIPrefBranch); Prefs.setcharpref (' browser.startup.homepage ', url); }} </script> </pead> <body> set Homepage-Favorites </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.