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! ');
}
}