JS Firefox Add Favorites feature code compatible with Firefox and Ie_javascript tips

Source: Internet
Author: User

The complete code is as follows: Chrome also has tips, to tell the truth generally this type of code is commonly used under IE, other browsers are just to give a hint.

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

Here are some specific explanations:

1, IE browser to add the code of Favorites
Window.external.addFavorite (sURL, stitle);
Parameters:
sURL: Full Site page address added to Favorites
Stitle: The name of the Site page title added to the Favorites folder.
Example:
<a href= ' # ' onclick= ' javascript:window.external.AddFavorite (' http://www.domain.com ', ' name '); " > Add to Favorites </a>

2, Firefox browser to add the code of favorites
Firefox is called bookmark (favorites), the code has two ways, a similar approach to IE browser, It is only window.external.AddFavorite replaced with Sidebar.addpanel, another one is very simple, only need to add a parameter on it.
The first method:
Window.sidebar.addPanel (Stitle, sURL, "");
Parameters
sURL: Collection of Web pages full connection
Stitle: The name of the favorite Web page
Example:
<a href= ' # ' onclick= ' Javascript:window.sidebar.addPanel (' name ', ' http://www.domain.com ', '); " > Favorites this site </a>
Note here that the order of the parameters of the AddPanel function is exactly the opposite of the addfavorite.
The second method:
Add the Rel= "sidebar" property to the link
Example:
<a href=http://www.domain.com title= "name" rel= "sidebar" > Favorites site </a>
Note: The title property here is the name of the favorite Site page.
3, compatible with Firefox and IE browser methods
Now that Firefox offers two ways, there are two ways we can be compatible with Firefox and IE browsers.
The first method:
Here is an alternative way to solve this problem, the code is more clear. This is the best way to do it!

<script> function Addfavorite () {if (document.all) {window.external.addFavorite (' http://www.domain.com '), ' Name '); else if (Window.sidebar) {window.sidebar.addPanel (' name ', ' www.domain.com ', ' "); }} </script> Add to Favorites
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Note that IE is the domain name in front, Firefox is the domain name in the back door, leaving a blank character last.
The second method:
This is the simplest way, only a word.
<a href= ' http://www.domain.com ' onclick= ' javascript:window.external.AddFavorite (' http://www.domain.com ', ' name ') ; "title=" name "rel=" sidebar "> Favorites site </a>
But there are some problems with this approach, which is to open the address in the link at the same time as IE. So let's try to optimize it!
<a href= "www.domain.com/" onclick= "Window.external.addFavorite (This.href,this.title); return false; title= ' name ' Rel= "sidebar" > Add to Favorites </a>
This is no problem, haha! At the same time compatible with IE6, IE7, Firefox, opera and other browsers.
For more compatibility with better code, you can search for cloud-dwelling community previous articles.

the bottom of the cloud-dwelling Community Favorites Homepage Feature
<script type= "Text/javascript" > Function Addbookmark (title,url) {if (Window.sidebar) {Window.sidebar.addPane L (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 operation 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 is 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> Set homepage-Favorites
[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.