Because chrome, safari, and opara Browsers Do not support automatic "set as Homepage" & "add to Favorites", we can only try and catch and give an error message! 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:
Copy codeThe 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:
Copy codeThe 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! ');
}
}