Home  >  Article  >  Web Front-end  >  Javascript code set as homepage and favorites (personal test is compatible with IE, Firefox, chrome and other browsers)_javascript skills

Javascript code set as homepage and favorites (personal test is compatible with IE, Firefox, chrome and other browsers)_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:14:32947browse

Since chrome, safari, and opara browsers do not yet support automatic "set as homepage" & "add to favorites". So we can only try, catch, and give an error message! It's perfect.

In addition, in IE, many people have also encountered the problem that window.external.addFavorite. will report an error. So I had to throw a prompt message.

Add to favorites:

Copy code The 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 use Ctrl D to add" ); addPanel(document.title, window.location.href, "");
}else{
}else{
} alert( "Failed to add to favorites, please use Ctrl D to add" );
}
}



Set as homepage:

Copy code The 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 feature, please enter about:config in the address bar, and then sign the item. The value of applets.codebase_principal_support should be true" );
                                                                                                                     ;
prefs.setCharPref('browser.startup.homepage',window.location.href);
}else{
alert('Your browser does not support automatic setting of homepage, please use your browser Menu manual settings!');
}
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn