简体   繁体   English

如何仅在首次访问时显示页面加载时带有slimbox的弹出窗口?

[英]How can I show popup with slimbox on page load only for the first visit?

So far my idea was to implement a function popup("url") checking for an existing cookie which is triggered by loading the body of the page (onLoad - not a nice solution I know). 到目前为止,我的想法是实现一个功能popup(“ url”),以检查是否存在一个现有的cookie,该cookie是通过加载页面的正文触发的(onLoad-我知道这不是一个很好的解决方案)。 I used the exact code from here to implement cookie functionality which I included in my application.js . 我使用这里的确切代码来实现cookie功能,该功能已包含在application.js中。 There I also included my popup() function: 我还包括了我的popup()函数:

   function popUp(startimage) {
    var x = readCookie('ecocrowd')
    if (x = 'nopopup') {
    }
    else {
        jQuery.slimbox(startimage);
    }

}

In the html-template for my start page I included the following parts: 在我的起始页的html模板中,我包括以下部分:

<body onLoad="popup("wirdiezukunft.png")"> 

and

<script>createCookie('ecocrowd','nopopup',7)</script>

Am I missing something out? 我错过了什么吗? It is the first time I am working with a Javascript implementation like this so I would appreciate any kind of help. 这是我第一次使用这样的Javascript实现,因此我将不胜感激。 Thanks in advance! 提前致谢!

update (content of createcookie() ): 更新(createcookie()的内容):

function createCookie(name,value,days) {


    if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }

Forget cookies, use localStorage as it has a much more practical interface, and no weird stuff with paths, expirations and domains: 忘记cookie,使用localStorage,因为它具有更实用的界面,并且没有奇怪的东西,包括路径,到期时间和域:

if(!window.localStorage.getItem('hasShown')) {
  showMyPopup();
  window.localStorage.setItem('hasShown', true);
}

There is actually a much more practical interface for cookies in HTML5 but it isn't as widely supported as localStorage yet. 实际上,HTML5中有一个用于cookie的实用得多的接口,但是还没有localStorage广泛支持。

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

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