不明白楼主楼实现什么效果, 用JS控制权限作用不大, 如果客户禁用JS怎么办?为什么把权限写在<body>里的<input里, 而且不直接写在<script>里,如:
<script>
...
function checkRole(k)
{
var key= k;
...
}
checkRole(3);</script>按楼主的方法可以把checkRole函数放在要取值的<input下方即可
<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>
<script>
...
function checkRole(k)
{
var key= k;
...
}
checkRole(3);</script>按楼主的方法可以把checkRole函数放在要取值的<input下方即可
<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>
如果在没有构造body之前进行检查这句话没有意义,永远也得不到这个结点
var key= document.getElementById("txtValue").value
<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脚本就完了.
权限弄成js去验证,确实非常不好。但没办法。我信息的详细页面都已经生成了静态页面。除使用ajax的 xmlhttprequest进行验证外。我实在想不会其他法。
<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好了
<html>
<head>
<title>
页面标题
</title>
<script type="text/javascript">
function checkRole(k)
{
var key= kif(key=="3")
{
if(confirm('当前页面要求您登录,点击确定进行登录,取消返回首页'))
{
document.location.href="/user/userlogin.shtml";
}
else
{
document.location.href="/index.shtml";
}
}
}
checkRole(3); //把值写这儿
</script></head>
<body>
页面内容
中华人民共和国
</body>
</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>
checkRole()
</script>
页面内容
</body>
</html>
利用HTML执行的顺序来实现你要的效果,在执行BODY下面的页面内容之前就执行了checkRole()事件判断,如果通过会继续,如果没通过就会跳转了