1

Okay, I'm new to javascript coding, cookies etc., and I can't quite find the answer to my problem on the net. I'm trying to create a site that has a div that displays some helpful information at the top.

<div id="helpdiv">
<!--This content shows only on web browsers Internet Explorer 6/7/8/9/10/11 and Microsoft Edge.-->
Looks like your using Internet Explorer/Edge. This site is optimized when "Compatibility Mode" is disabled. Thank you!
</div>

I found some code that I can use that will show this div for 8 seconds, then disappear. But I want this to only show up once.

function closeHelpDiv(){
 document.getElementById("helpdiv").style.display=" none";
 }

 // close the div in 8 secs
 window.setTimeout( closeHelpDiv, 8000 );

I figured if a cookie was used, then the browser could check for that cookie, and if it existed, then it wouldn't need to show the div. Only the first time they visited the site.

So here's the flow I'm trying to acheive:

  1. Check for a cookie named “helpText”

  2. If the cookie doesn’t exist:

I want to run a function that hides a div (id="helpdiv") after 8 seconds of showing.

Here is some code I found that hides a div:

 function closeHelpDiv(){
 document.getElementById("helpdiv").style.display=" none";
 }

 // close the div in 8 secs
 window.setTimeout( closeHelpDiv, 8000 );

I then want to set a site cookie called ”helpText” so that next time they visit the site, the function won’t run again.

  1. If cookie exists:

I want the div with an Id of “helpdiv” to have the style=“display:none;”

If I need to add anymore code, please let me know and I can explain more. Any help would be a life saver!!

gdeleon101
  • 98
  • 1
  • 2
  • 11
  • 1
    1) Make your `helpdiv` invisible initially (`display:none`). 2) In `window.onload` handler parse `document.cookie` to find `helpText`. 3) If not found show `helpdiv` for 8 sec. 4) in `closeHelpDiv` set cookie. – Alex Kudryashev Jul 20 '16 at 20:15
  • 1
    Possible duplicate of [What is the "best" way to get and set a single cookie value using JavaScript](http://stackoverflow.com/questions/260749/what-is-the-best-way-to-get-and-set-a-single-cookie-value-using-javascript) – Heretic Monkey Jul 20 '16 at 20:24

4 Answers4

1

You can check for cookies on the current web document like this:

document.cookie

So if you are planning to check for a specific string you could do an indexOf("") with the word you are looking for and validating if the index is more than 0.

if(document.cookie.indexOf("helpText") > 0 ){
   the cookie was found, so your function should be here
}else{
   cookie not found
}

Probably is better to do a search over Stackoverflow because there are a lot of answers about cookies and javascript:

Here It's a full answer about this: Check if cookie exists else set cookie to Expire in 10 days

Community
  • 1
  • 1
Diego Ivan
  • 15
  • 5
0

try

function getCookie(name) {
  var value = "; " + document.cookie;
  var parts = value.split("; " + name + "=");
  if (parts.length == 2) return parts.pop().split(";").shift();
}

from

Get cookie by name

Use it to check whether the cookie exists or not. In your code you can easily

 if(getCookie('helpText')!=''){
   $('selector').css('attrib','prop');
 }
Community
  • 1
  • 1
Lemonade
  • 503
  • 2
  • 8
0

Extending on Lemmy's answer, this is what you need:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript"> 
    $(document).ready(function(){
        var myCookie = getCookie("helpText");

        if (typeof myCookie === 'undefined') {
            // close the div in 8 secs
            window.setTimeout( closeHelpDiv, 8000 );
        }


        function closeHelpDiv(){
            document.getElementById("helpdiv").style.display=" none";
        }

        function getCookie(name) {
            var value = "; " + document.cookie;
            var parts = value.split("; " + name + "=");
            if (parts.length == 2) return parts.pop().split(";").shift();
        }

    });
</script>  

Here is the updated script for your wordpress environment:

<script type="text/javascript"> 
    jQuery(document).ready(function($){
        var myCookie = getCookie("helpText");

        if (typeof myCookie === 'undefined') {
            // close the div in 8 secs
             window.setTimeout( closeHelpDiv, 8000 );
            //setTimeout(closeHelpDiv, 2000);
        }


        function closeHelpDiv(){
            document.getElementById("helpdiv").style.display=" none";
        }

        function getCookie(name) {
            var value = "; " + document.cookie;
            var parts = value.split("; " + name + "=");
            if (parts.length == 2) return parts.pop().split(";").shift();
        }
    });
</script>

In Wordpress you must use change the $ sign with jQuery and pass the $ sign into the function. The dollar sign in $(document).ready(function(){}); is not used for compatibility with other libraries.

GunWanderer
  • 324
  • 1
  • 7
  • I've added this code right before my closing

    tag and my div just stays there. Not sure if it is working or not.

    – gdeleon101 Jul 21 '16 at 04:26
  • Did you add a reference to the jQuery library? I've updated my comments above. – GunWanderer Jul 21 '16 at 13:58
  • Here is the jquery in my current header.php file: http://haskelacc.staging.wpengine.com/wp-includes/js/jquery/jquery.js and: http://haskelacc.staging.wpengine.com/wp-includes/js/jquery/jquery-migrate.min.js – gdeleon101 Jul 21 '16 at 18:26
  • Change `$(document).ready(function(){` To: `jQuery(document).ready(function($){` – GunWanderer Jul 21 '16 at 18:45
  • Thanks for the Wordpress update. Looks like the div hides as it should after 8 seconds, however every time I click on a new link within the site, the same div keeps appearing. I thought it wouldn't because of a cookie? – gdeleon101 Jul 22 '16 at 18:24
  • Then you gotta set the cookie in order for it to have a value. Check out the updated code here: [https://jsfiddle.net/GunWanderer/9Lx33mf5/](https://jsfiddle.net/GunWanderer/9Lx33mf5/) Mark this answer correct if it is! – GunWanderer Jul 22 '16 at 20:53
  • Sorry, still not working. It just flashes the div for half a sec. I wonder if we need to hide the helpdiv by default, then only show it if you DON'T have the cookie called helpText. Here is my link with your code in it. By the way, you can onlt see the div if you are in Chrome or IE. http://haskelacc.staging.wpengine.com/ – gdeleon101 Jul 23 '16 at 17:33
  • Thanks for all your help. I updated the code. Removed the hide div after 8 seconds, and added a close button. Everything runs in Chrome, cookies and all. However, IE still doesn't take the cookie. – gdeleon101 Jul 25 '16 at 15:42
  • It's your computer settings. Check out this thread: [http://stackoverflow.com/questions/24148735/ie-not-saving-cookie](http://stackoverflow.com/questions/24148735/ie-not-saving-cookie) – GunWanderer Jul 26 '16 at 16:30
  • Your right! That definitely looks like the culprit. My IE was checked. However that doesn't help my cause because now I have to worry about users having this checked by default, and my cookie idea now won't work for those users. And they are the users I'm trying to target with this popup. – gdeleon101 Jul 26 '16 at 22:12
  • Don't worry about it. In web development, you have no jurisdiction over the custom settings of the end-users. For example, if I enable popup blockers, you cannot code to disable it. We support the masses, not the few. – GunWanderer Jul 27 '16 at 14:50
0

So you need to set a cookie after displaying banner to user on her first visit -

function closeHelpDiv(){
    document.getElementById("helpdiv").style.display=" none";
    document.cookie="visitedBefore=true; expires=1 Jan 9999 12:00:00 UTC; path=/";
}

check with following code if that user already visited your site

function showBanner(){
    // check if visited Earlier
    if(!getCookie('visitedBefore'))){
        window.setTimeout( closeHelpDiv, 8000 );
    }   
}

function getCookie(name) {
   var value = "; " + document.cookie;
   var parts = value.split("; " + name + "=");
  if (parts.length == 2) return parts.pop().split(";").shift();
}

  showBanner();

So you are doing all things correct just use cookies as described above.

paraS elixiR
  • 1,866
  • 1
  • 18
  • 26