There are more and more browsers, which makes the compatibility of web design more and more demanding, like commonly used to set the home page, to add the collection code, general although concise, but the compatibility is very bad, to join the collection and set the first page code almost every site in the head put on a, there is no effect first, the demand should be there.
However, due to browser compatibility issues, the previous use of many of the code has lost its effect, the following gives a section of the browser can be compatible with the code, and can not be considered compatible, can only say in the unsupported browser can give hints, the code is as follows:
<!doctype html>
<html>
<head>
<title>Add to Favorites and Set as Homepage</title>
<script type="text/javascript">
//Add to Favorites
Function AddFavorite(sURL, sTitle){
sURL = encodeURI(sURL);
Try{
Window.external.addFavorite(sURL, sTitle);
}
Catch(e){
Try{
Window.sidebar.addPanel(sTitle, sURL, "");
}
Catch(e){
Alert("Failed to add to the collection, please use Ctrl+D to add, or manually set it in the browser.");
}
}
}
//Setup as front page
Function SetHome(url){
If (document.all){
Document.body.style.behavior='url(#default#homepage)';
document.body.setHomePage(url);
}
Else{
Alert ("Hello, your browser does not support the automatic setting page for the home page function, please manually set this page as the home page in the browser!");
}
}
</script>
</head>
<body>
<a href="javascript:void(0)">Set Homepage</a>
<a href="javascript:void(0)">Add to Favorites</a>
</body>
</html>
The above code is brief and easy to understand, can be compatible with each browser to join the collection and set the homepage, there are questions welcome to put forward, small series will promptly contact with you, thank you!
Here's to share a piece of jquery code implementation add Favorites feature
//Setup as front page
Function SetHomePage() {
If (document.all) {
Document.body.style.behavior = 'url(#default#homepage)';
document.body.setHomePage('http://www.87cool.com');
}
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, please enter about:config in the address bar and then the signed.applets.codebase_principal_support value should be true");
}
}
Var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage', 'http://www.87cool.com');
}
}
//Add to Favorites
Function AddFavorite() {
Var title = document.title;
Var url = location.href;
If (window.sidebar) {
Window.sidebar.addPanel(title, url, "");
} else if (document.all) {
window.external.AddFavorite(url, title);
} else {
Return true;
}
}