
Javascript add to favorites and set as homepage(compatible with IE, Chrome and FireFox)
In order to let users bookmark our website for easy browsing next time, usually add "set as homepage and add to favorites" at the top of the website's homepage. These two functions are implemented by Javascript, the following is their code:
1. Set as homepage javascript(compatible with IE, Chrome and FireFox)
function SetAsHomePage(obj){
var url = window.location.href;
try{
//Set as homepage of the IE browser
obj.style.behavior = 'url(#default#homepage)';
obj.SetAsHomePage(url);
}
catch(e){
//Set as homepage of Chrome or Firefox browser
if(window.netscape){
try{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
}
catch (e){
alert("The browser refuses to set the current website as the homepage! To continue, please enter 'about:config' in the browser address bar and press Enter on your keyboard, then double-click [signed.applets.codebase_principal_support] to set its value to 'true'.");
}
var pre = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
pre.setCharPref('browser.startup.homepage',url);
}
}
}
Call: <a onclick="SetAsHomePage(this)">Set as homepage</a>
2. Javascript add to favorites(compatible with IE, Chrome and FireFox)
function addFavorite(){
var url = window.location.href;
try{
window.external.addFavorite(url, "Lionsure");
}
catch (e){
try{
window.sidebar.addPanel("Lionsure", url, "");
}
catch (e){
alert("Failed to add to favorites, please press Ctrl + D to add!");
}
}
}
Call: <a onclick="AddToFavorites()">Add to favorites</a>
-
Related Reading
- Javascript multidimensional array (create, add and r
- Javascript trim string whitespace, with removing lef
- Remove text after clicking in the textbox and then d
- Javascript get referrer url(previous page url), curr
- Javascript get current url, domain from url, relativ
- Javascript convert hex to decimal, binary to hex, oc
- Javascript delay loading images to improve web page
- Remove html element javascript by their parent or cl
- Javascript countdown and redirect after a few second
- Difference between substr and substring in javascrip
- Javascript split usage and examples, with splitting
- Javascript get all input elements in form and all im