兼容IE,Firefox,chrome等浏览器 : 设为首页和收藏的Javascript代码
2013-05-27 16:18
986 查看
由于chrome,safari,opara 浏览器还未支持自动 "设为首页" & "加入收藏". 所以我们只能try, catch一下, 给个错误提示! 也算完美了.
另外ie中, 很多人也碰到过 它window.external.addFavorite. 时会报错的问题. 所以俺也只好抛出提示信息了.
加入收藏:
function addFavorite(){
if (document.all){
try{
window.external.addFavorite(window.location.href,document.title);
}catch(e){
alert( "加入收藏失败,请使用Ctrl+D进行添加" );
}
}else if (window.sidebar){
window.sidebar.addPanel(document.title, window.location.href, "");
}else{
alert( "加入收藏失败,请使用Ctrl+D进行添加" );
}
}
设为首页:
/*
* author : 2010-12-27 11:33:02 teresa
* discription: set homepage
*/
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( "该操作被浏览器拒绝,如果想启用该功能,请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true" );
}
}
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage',window.location.href);
}else{
alert('您的浏览器不支持自动自动设置首页, 请使用浏览器菜单手动设置!');
}
}
另外ie中, 很多人也碰到过 它window.external.addFavorite. 时会报错的问题. 所以俺也只好抛出提示信息了.
加入收藏:
function addFavorite(){
if (document.all){
try{
window.external.addFavorite(window.location.href,document.title);
}catch(e){
alert( "加入收藏失败,请使用Ctrl+D进行添加" );
}
}else if (window.sidebar){
window.sidebar.addPanel(document.title, window.location.href, "");
}else{
alert( "加入收藏失败,请使用Ctrl+D进行添加" );
}
}
设为首页:
/*
* author : 2010-12-27 11:33:02 teresa
* discription: set homepage
*/
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( "该操作被浏览器拒绝,如果想启用该功能,请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true" );
}
}
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage',window.location.href);
}else{
alert('您的浏览器不支持自动自动设置首页, 请使用浏览器菜单手动设置!');
}
}
相关文章推荐
- 兼容IE,Firefox,chrome等浏览器 : 设为首页和收藏的Javascript代码
- 兼容IE,Firefox,chrome等浏览器 : 设为首页和收藏的Javascript代码
- 设为首页和收藏的Javascript代码(亲测兼容IE,Firefox,chrome等浏览器)
- 兼容IE和Firefox的设为首页和收藏的Javascript代码
- 兼容IE和Firefox的设为首页和收藏的Javascript代码
- [转] 兼容IE和Firefox的设为首页和收藏的Javascript代码
- 设为首页加入收藏代码_兼容各浏览器ie系列Firefox
- 兼容IE和Firefox的设为首页和收藏的Javascript代码
- 设为首页加入收藏代码_兼容各浏览器ie系列Firefox
- 兼容IE和Firefox的设为首页和收藏的Javascript代码
- 兼容IE和Firefox的设为首页和收藏的Javascript代码
- 设为首页/加入收藏代码_兼容各浏览器ie系列 Firefox/360 部分不兼容 请自己尝试
- 兼容IE和Firefox的设为首页和收藏的Javascript代码
- 兼容IE和Firefox的设为首页和收藏的Javascript代码
- javascript获取select的当前值示例代码(兼容IE/Firefox/Opera/Chrome)
- JS兼容Firefox IE加入收藏 设为首页代码
- 加入收藏 设为首页 代码:兼容IE和FireFox
- javascript 加入收藏、设为首页(IE,firefox兼容脚本之家版)
- 设为首页加入收藏兼容360/火狐/谷歌/IE等主流浏览器的代码 详细出处参考:http://www.jb51.net/article/35063.htm