0

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). I used the exact code from here to implement cookie functionality which I included in my application.js . There I also included my popup() function:

   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:

<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. Thanks in advance!

update (content of 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=/";
    }
chrstoph
  • 1
  • 1

1 Answers1

1

Forget cookies, use localStorage as it has a much more practical interface, and no weird stuff with paths, expirations and domains:

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.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • so instead of showMyPopup(); I would simply insert my function jQuery.slimbox("image.jpg"); ? I never worked with localStorage so I am not sure how to handle it. Would I paste this code as script in my html template? Thanks for your help! – chrstoph Jul 14 '15 at 10:16