If you want to be compatible with multiple browsers to determine the user's browser type, if it is Firefox or ie6,7,8, so that they can not be judged by the difference between the operation.
<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 ("Join collection failed, please use Ctrl+d to add");
}
}
}
function Sethome (OBJ,VRL) {
try{
Obj.style.behavior= ' url (#default #homepage) '; Obj.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>
<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>
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 operation is rejected by the browser. If you want to enable this feature, 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 (components. Interfaces.nsiprefbranch);
prefs.setcharpref (' Browser.startup.homepage ', document.url);
}
}
Call method
<a href= "#" onclick= "setHomePage ();" > Homepage </a>
<a href= "Javascript:addtofavorite ();" > Add Favorites </a>