简体   繁体   English

如何防止全局javascript变量在每次页面加载时初始化?

[英]How do I prevent a global javascript variable from initializing on every page load?

I am trying to display a div-tag when someone first comes to my site. 有人首次访问我的网站时,我试图显示一个div标签。 After clicking on links to other pages, when they come back to the home page, I don't want that div-tag displayed. 单击指向其他页面的链接后,当它们返回首页时,我不希望显示该div标签。 Is there any way I can save something like a session variable, which I can test so that I don't show the introductory div-tag? 有什么办法可以保存诸如会话变量之类的东西,可以对其进行测试,以便不显示div标签标签?

I tried using a global declaration outside of a function, but that initializes every time the page loads. 我尝试使用函数外部的全局声明,但是每次页面加载时都会初始化。 Since it is a .php page, it is going to reload, of course. 既然是PHP页面, 会重新加载,当然。

Is there any other way to do this (other than cookies), for example by executing the javascript code from php (I know how to do session variables in PHP). 还有其他方法可以做到这一点(除了cookie之外),例如通过从php执行javascript代码(我知道如何在PHP中执行会话变量)。

If you for whatever reason do not want to use storage, and take the hit on php pageload of reading your preventive variable, you could just post it to whichever pages your users are going to, and have those pages post it to your original, first script when they come back. 如果您出于某种原因不想使用存储,并且对读取预防变量的php pageload产生了兴趣,则可以将其发布到用户要访问的任何页面上,然后将这些页面首先发布到原始页面上他们回来时的脚本。 Awkward, and lots of code but if your question is ways to achieve the thing you describe, it works. 笨拙,有很多代码,但是如果您的问题是实现所描述内容的方法,那么它就可以工作。

First Thanx to @Denys Séguret for providing the answer to my question. 首先感谢@DenysSéguret提供了我的问题的答案。 Here is the code I eventually created. 这是我最终创建的代码。

First, in the Header of the .php file, I have: 首先,在.php文件的标题中,我有:

    <script type="text/javascript">
    if (sessionStorage.getItem('firsttime') === null) {
        sessionStorage.setItem('firsttime', 'True');
    }
    function Hider(){
        document.getElementById("Opener").style.display = "none";
        sessionStorage.setItem('firsttime', 'False');
        }
    </script>

Then in the body, I have: 然后在体内,我有:

 <script type="application/x-javascript"> var IsItFirstTime = sessionStorage.getItem('firsttime'); varMessage1 = "<p class=\\"h1\\">Welcome to Blah-Bla!</h1><br><h2>The premier site for the best movies!</h2>" varMessage2 = "<p>Every week we bring you a fresh batch of movies and you can expect these to be the best movies in that category.</p><p>Click to Continue</p>" if (IsItFirstTime == 'True') { var1 = "<div id=\\"Opener\\" onclick=\\"Hider()\\">" + varMessage1 + varMessage2 + "</div>"; document.write(var1); } </script> 

And this is peripheral, but here is the CSS: 这是外围设备,但这里是CSS:

 #Opener { background-color: #ff0099; position:absolute; width:600px; height:440px; z-index:100; margin-left: 50%; left: -300px; top: 180px; text-align:center; padding-left:50px; padding-right:50px; } 

Note the z-index line, which floats the "Opener" div on the page. 请注意z-index行,该行在页面上浮动“ Opener” div。

Hope this helps someone else. 希望这对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM