JacobTheDev
JacobTheDev

Reputation: 18520

jQuery Cookie Question

I've looked around a lot, and can't seem to find anything that's simple enough for me to do...

I've set up a web page that detects which browser is currently running, and if it's something other than Firefox 4.0, it displays a hidden div that gives a warning stating that the page is best viewed in Firefox 4.0 or greater. Within that div is a button that hides the div onclick.

I'm looking for a way to remember that this button has been clicked during a session, so that when a user clicks on my "home" page, they don't get the same message every time.

Current code:

<head>
    <script src="js/browsercheck.js"></script>
    <script type="text/javascript">
        // external script "browsercheck.js" checks
        // which browser/version is being used
        // check browser and display message if != Firefox 4.0 or >
        function checkBrowser() {
            var browser = BrowserDetect.browser;
            var version = BrowserDetect.version;
            if (browser == "Firefox") {
                if (version >= 4) {
                    // do nothing
                }
            } else {
                document.getElementById("coverall").style.visibility="visible";
                document.getElementById("browser").innerHTML = browser + " " + version;
            } 
        }
        // remove overlay if user commands
        function removeCoverall() {
            document.getElementById("coverall").style.visibility="hidden";
        }
    </script>
</head>
<body>
    <div id="coverall" style="visibility:hidden;">
        <p>I see you're using <span id="browser"></span>. Please use Firefox.</p>
        <button type="button" onclick="removeCoverall()">I understand</button>
    </div>
</body>

Upvotes: 0

Views: 321

Answers (3)

Khez
Khez

Reputation: 10350

You seem to have the right idea, you need cookies! YUM!

JS

function removeCoverall() {
    var date = new Date();
    date.setTime(date.getTime()+(7*24*60*60*1000));// expires in one week
    document.cookie = 'skipNotify=1;expires='+date.toGMTString()+'; path=/';
    document.getElementById("coverall").style.visibility="hidden";
}

Now retrieving the cookie can be difficult in javascript, but you can use PHP!

PHP

function checkBrowser() {
    <?php if(isset($_COOKIE['skipNotify']))echo'return;';?>
    var browser = BrowserDetect.browser;
    var version = BrowserDetect.version;
    if (browser == "Firefox") {
        if (version >= 4) {
            // do nothing
        }
    } else {
        document.getElementById("coverall").style.visibility="visible";
        document.getElementById("browser").innerHTML = browser + " " + version;
    } 
}

The above code injects a return statement if the user has the cookie, so the call to the function won't do anything.

As mentioned in the other posts, I highly recommend you use jQuery for all your javascript needs. It's very popular, stable and useful! I only gave my answer as it does not use any third party solution.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

In the removeCoverall function you could set a cookie which indicates that the user closed the div and in checkBrowser function verify if the cookie is present before showing the div.

Upvotes: 0

Bosh
Bosh

Reputation: 8738

Using jQuery and the cookie plugin, you can do:

 function removeCoverall() {
            $.cookie("user_clicked_ok", "true");
            document.getElementById("coverall").style.visibility="hidden";
        }

$(window).ready(function() {
  if ($.cookie("user_clicked_ok")=="true") {removeCoverall();}
});

More details at: http://www.electrictoolbox.com/jquery-cookies/

Upvotes: 1

Related Questions