Javascript与会话显示或隐藏内容

Javascript with session to show or hide content

本文关键字:隐藏 显示 会话 Javascript      更新时间:2023-09-26

我在做电影网站。

我要求"基础"脚本检查是否某些用户登录或没有访问视频播放器。

问题陈述:目前显示即使用户没有登录,我也想更改。

到目前为止,我想出了这段代码,但它总是返回true,因为我的var将$_Session的值作为文本。

  <p>Click the check user:</p>
  <button onclick="checkuser()">Try it</button>
  <p id="demo"></p>
  <script>
  function checkuser() 
  {
     var greeting;
         var userlog = '<%= Session["SessionKey"] %>';  // dreamweaver error here

     if (userlog==null)
          {
           greeting = "Bye";        // here - i will not show content
          } 
         else {
           greeting ="Go ahead";    // here - i will show content
          }
     document.getElementById("demo").innerHTML = greeting;
   }
   </script>

您正在检查null是否永远不会为真。如果<%= Session["SessionKey"] %>为空,userlog将被设置为空的string而不是null

试试……

if (userlog == '')

使用sessionStorage检查登录用户:

On Login Button:

if(typeof(Storage) !== "undefined") {
sessionStorage.setItem("Uname", "LoggedInUserName");
    }

然后:

function checkuser()
{
var user= sessionStorage.getItem("Uname");
if(user==null)
greeting="Bye"
else
{
  greeting="Go Ahead"
}