不明白楼主楼实现什么效果, 用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>

解决方案 »

  1.   

    没有看明白
    如果在没有构造body之前进行检查这句话没有意义,永远也得不到这个结点
    var key= document.getElementById("txtValue").value
      

  2.   

    那就打破一下常规.
    <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脚本就完了.
      

  3.   


    权限弄成js去验证,确实非常不好。但没办法。我信息的详细页面都已经生成了静态页面。除使用ajax的  xmlhttprequest进行验证外。我实在想不会其他法。
      

  4.   

    <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>
      

  5.   

    body嵌套一个div在里面,不显示的时候就清空里面内容
      

  6.   


    既然页面是动态生成的,那就把参数直接传给 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>
      

  7.   

    恩,霍然开朗,但这样能确保在呈现body内容前,先执行checkRole吗?》
      

  8.   

    首先要说明的是,这种通过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>
      

  9.   

    <!--以下代码纯手工编写。仅提供问题表述,大家帮看看-->
    <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()事件判断,如果通过会继续,如果没通过就会跳转了
      

  10.   

      涉及到权限问题都可以使用COOKIE
      

  11.   

    你直接让这个页面出不来换个页面就好了啊,可以在action里面去处理啊