我想用javascript实现
网页一打开时或关闭时就提示设置主页,网上看了查找了好多资料,都是说在body onload和onunload时调用一个方法就可以了,但是我很奇怪,我也调用了,但是根本没有效果,提示“没有权限”。
代码基本上是没有问题了,有人说是IE安全性设置的问题,我更改了IE安全等级,打开或关闭页面时,还是不提示设置主页。请大家帮忙看一下代码,看要怎么解决。
谢谢!
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>设置首页和添加到收藏夹</title>
</head>
<body><!-- 这里面的代码测试时提示没有权限-->
<body id="iehomepage" onload="onLoad()" onunload="onUnload()"> <script>
// 页面一打开时就提示要设置主页
function onLoad() {
iehomepage.style.behavior='url(#default#homepage)';
if(!(iehomepage.isHomePage('http://www.qq.com')))
iehomepage.setHomePage('http://www.qq.com');
}
// 页面关闭时提示设置主页
function onUnload() {
iehomepage.style.behavior='url(#default#homepage)';
if(!(iehomepage.isHomePage('http://www.qq.com')))
iehomepage.setHomePage('http://www.qq.com');
}
</script></body>
</html>
网页一打开时或关闭时就提示设置主页,网上看了查找了好多资料,都是说在body onload和onunload时调用一个方法就可以了,但是我很奇怪,我也调用了,但是根本没有效果,提示“没有权限”。
代码基本上是没有问题了,有人说是IE安全性设置的问题,我更改了IE安全等级,打开或关闭页面时,还是不提示设置主页。请大家帮忙看一下代码,看要怎么解决。
谢谢!
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>设置首页和添加到收藏夹</title>
</head>
<body><!-- 这里面的代码测试时提示没有权限-->
<body id="iehomepage" onload="onLoad()" onunload="onUnload()"> <script>
// 页面一打开时就提示要设置主页
function onLoad() {
iehomepage.style.behavior='url(#default#homepage)';
if(!(iehomepage.isHomePage('http://www.qq.com')))
iehomepage.setHomePage('http://www.qq.com');
}
// 页面关闭时提示设置主页
function onUnload() {
iehomepage.style.behavior='url(#default#homepage)';
if(!(iehomepage.isHomePage('http://www.qq.com')))
iehomepage.setHomePage('http://www.qq.com');
}
</script></body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
</style>
</head>
<body>
<a href="#" onclick="addBook()">添加书签</a>
<a href="#" onclick="setHome()">设为首页</a>
<script>
function addBook(){
var title = '百度一下',
url = 'http://www.baidu.com';
if(document.all){
try{
window.external.addFavorite(url, title);
} catch (e1){
try{
window.external.addToFavoritesBar(url, title);
} catch (e2){
alert('加入收藏失败,请您手工加入。');
}
}
} else if (window.sidebar){
window.sidebar.addPanel(title, url, '');
} else {
alert('加入收藏失败,请您手工加入。');
}
} function setHome(){
var obj = document.body,
vrl = 'http://www.baidu.com';
try{
obj.style.behavior = 'url(#default#homepage)';
obj.setHomePage(vrl);
} catch (e){
if (window.netscape){
try{
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
} catch (e){
alert("此操作被浏览器拒绝!\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
}
var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage', vrl);
}
}
}
</script>
</body>
</html>
楼主参考下