JavaScript compatible IE,FF settings home, add favorite code

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags .url address bar ajax browser browsers code difference document

If you want to be compatible with multiple browsers, you should judge the user's browser type, if it is Firefox or ie6,7,8, and so on, so that they can not be judged by the difference between the operation.

<html xmlns= "http://www.w3.org/1999/xhtml" >
<head runat= "Server" >
<title> Untitled page </ title>
<script language= "Web Effects" type= "Text/javascript" >
function Addfavorite (sURL, Stitle)
{
Try
{
Window.external.addfavorite (sURL, stitle);
}
catch (e)
{
Try
{
Window.sidebar.addpanel (stitle, sURL, "");
}
catch (e)
{
Alert ("Failed to join the collection, please use CTRL to add");
}
}
}
function Sethome (OBJ,VRL) {
try{
obj.style.behavior= ' url (#default #homepage) '; o Bj.sethomepage (VRL);
}
catch (e) {
if (window.netscape) {
try {
Netscape.security.privilegemanager.enableprivilege (" Universalxpconnect ");
}
catch (e) {
Alert ("This operation is rejected by the browser!") n Please 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 ', VRL);
}
}
}
</script>

</head>
<body>
<form id= "Form1" runat= "Server" >
<a onclick= "Addfavorite (window.location,document.title)" style= "Cursor:pointer" > Add Favorites </a>
<a onclick= "Sethome (this,window.location)" style= "Cursor:pointer" > Set as Homepage </a>
</form>
</body>
</html>
Simple code

Add to Favorites
function Addtofavorite ()
{
if (document.all) {
Window.external.addfavorite (Document.url,document.title);
}else if (Window.sidebar) {
Window.sidebar.addpanel (Document.title, Document.url, "");
}
}

//Set as homepage
Function sethomepage () {
 if (document.all) {
    Document.body.style.behavior= ' url (#default #homepage) ';
    document.body.sethomepage (Document.url);
 }else if (window.sidebar) {
  if (window.netscape) {
       try{
          Netscape.security.privilegemanager.enableprivilege (" Universalxpconnect ");
      }catch (e) {
      alert ("This action is rejected by the browser, and if you want to enable this feature, Please enter about:config in the Address bar and then signed.applets.codebase_principal_support the value to true ");
      }
 }
    var prefs = components.classes[' @mozilla. Org/preferences-service;1 '].getservice (rs. Interfaces.nsiprefbranch);
    prefs.setcharpref (' Browser.startup.homepage ', document.url);
 }
}
Calling method

<a href= "#" onclick= "setHomePage ();" > Homepage </a>
<a href= "Javascript:addtofavorite ();" > Add Favorites </a>
 

Related Article

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.