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>