onload前的执行方法
当我们静态页面在呈现body内容前,我要先执行一个函数来判断是否符合查看内容条件,只有符合查看条件后,才会显示body内容要怎么写才好。我用body onload=""
它是内容已经显示出来了,换其他事件又找不出什么事件才好。
<!--以下代码纯手工编写。仅提供问题表述,大家帮看看--><html><head><title>页面标题</title><script type="text/javascript">function checkRole(){var key= document.getElementById("txtValue").value;if(key=="3"){ if(confirm('当前页面要求您登录,点击确定进行登录,取消返回首页')){ document.location.href="/user/userlogin.shtml";}else{ document.location.href="/index.shtml";}}}</script></head><body>页面内容<input type="text" id="txtValue" value="3"/></body></html><html><head><title>页面标题</title><script type="text/javascript">function checkRole(){var key= document.getElementById("txtValue").value;if(key=="3"){ if(confirm('当前页面要求您登录,点击确定进行登录,取消返回首页')){ document.location.href="/user/userlogin.shtml";}else{ document.location.href="/index.shtml";}}}</script></head><body>页面内容<input type="text" id="txtValue" value="3"/><script> checkRole();</script>中华人民共和国</body></html>
[解决办法]
那就打破一下常规.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript" type="text/javascript">
function check() {
if (document.getElementById('v1').value == '1') {
alert('...');
} else {
alert('???');
window.opener = null;
window.close();
}
}
</script>
</head>
<body>
<input type="hidden" id="v1" name="v1" value="3" />
<script language="javascript" type="text/javascript">
check()
</script>
ddd
ddd
ddd
</body>
</html>
不过人家禁用了JS脚本就完了.
[解决办法]
<html><head><title>页面标题</title><script type="text/javascript">function checkRole(){var key= document.getElementById("txtValue").value;if(key=="3"){ if(confirm('当前页面要求您登录,点击确定进行登录,取消返回首页')){ document.location.href="/user/userlogin.shtml";}else{ document.location.href="/index.shtml";}}}</script></head><body>页面内容<input type="text" id="txtValue" value="3"/><script> checkRole();</script>中华人民共和国</body></html>
[解决办法]
首先要说明的是,这种通过JS来控制权限的方法很不严密,查看页面源码就可以看到内容。
以下是按楼主要求的解决方案,其实很简单:先将<body>之间的内容用display:none隐藏,判断值后再决定是否显示。以下代码测试通过:
<html><head><title>页面标题</title><script type="text/javascript">function checkRole(){ var key = document.getElementById("txtValue").value; if(key == "3") { if(confirm('当前页面要求您登录,点击确定进行登录,取消返回首页')) document.location.href="/user/userlogin.shtml"; else document.location.href="/index.shtml"; } else document.body.style.display = "block";//显示页面内容}</script></head><body onLoad="checkRole()" style="display:none">页面内容<input type="text" id="txtValue" value="2"/></body></html>
[解决办法]
<!--以下代码纯手工编写。仅提供问题表述,大家帮看看--><html><head><title>页面标题</title><script type="text/javascript">function checkRole(){var key= document.getElementById("txtValue").value;if(key=="3"){ if(confirm('当前页面要求您登录,点击确定进行登录,取消返回首页')){ document.location.href="/user/userlogin.shtml";}else{ document.location.href="/index.shtml";}}}</script></head><body><input type="text" id="txtValue" value="3"/><script>[color=#FF0000]checkRole()[/color]</script>页面内容</body></html>