Set as the homepage and the Javascript code added to the favorites (Pro-tested and compatible with IE, Firefox, chrome, and other browsers) _ javascript skills

Source: Internet
Author: User
This article mainly introduces the Javascript code set as the homepage and favorites (it is compatible with IE, Firefox, chrome and other browsers ). For more information, see chrome, safari, and opara. the browser does not support "set as Homepage" and "add to Favorites ". so we can only try and catch it and give an error prompt! It's perfect.

In Internet Explorer, many people have encountered an error in window. external. addFavorite., so I had to throw a prompt.

Add to favorites:

The Code is as follows:


Function addFavorite (){
If (document. all ){
Try {
Window. external. addFavorite (window. location. href, document. title );
} Catch (e ){
Alert ("failed to add to favorites, please add with Ctrl + D ");
}

} Else if (window. sidebar ){
Window. sidebar. addPanel (document. title, window. location. href ,"");
} Else {
Alert ("failed to add to favorites, please add with Ctrl + D ");
}
}


Set as homepage:

The Code is as follows:


Function setHomepage (){
If (document. all ){
Document. body. style. behavior = 'url (# default # homepage )';
Document. body. setHomePage (window. location. href );
} Else if (window. sidebar ){
If (window. netscape ){
Try {
Netscape. security. PrivilegeManager. enablePrivilege ("UniversalXPConnect ");
} Catch (e ){
Alert ("this operation is rejected by the browser. If you want to enable this function, enter about: config in the address bar, and set the value of signed. applets. codebase_principal_support to true ");
}
}
Var prefs = Components. classes ['@ mozilla.org/preferences-service%1'}.getservice (Components. interfaces. nsIPrefBranch );
Prefs. setCharPref ('browser. startup. homepage', window. location. href );
} Else {
Alert ('your browser does not support automatic homepage settings. Please use the browser menu to manually set it! ');
}
}

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.