Search code examples
javascriptdiscourse

How to only display banner to first time visitor?


I have this text banner on my site. I want it to show it only to new visitors using cookies. I have very little knowledge of JS so please help me how can I show it to only first-time visitors.

I am using it on Discourse-based this Android forum site which is already built on Node.js.

<style>
.top  {
  background: linear-gradient(to right, #141517, #6A9113);
    border-radius: 10px;
    font-size: 20px;
    color: white;
    opacity: 0.9;
    text-align: center;
    padding: 25px;
    line-height: 30px;
    margin: 10px 0px 30px 0px;

}
</style>


<div class="top">
Welcome to the forum. This is demo text.
</div>

Solution

  • Here is the full code... Working demo https://jsfiddle.net/hdas2012/6yjo346k/

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
    
    <div class="top">
    <h2>You will only see it once</h2>
      Welcome to the forum. This is demo text.
    </div>
    
    <script>
        $(document).ready(function(){
          if(!$.cookie('welcomeMsg')){
            $(".top").show();
            $.cookie('welcomeMsg', 'Y', { expires: 365*3 });
          }
        });
    </script>
    <style>
        .top  {
          background: linear-gradient(to right, #141517, #6A9113);
            border-radius: 10px;
            font-size: 20px;
            color: white;
            opacity: 0.9;
            text-align: center;
            padding: 25px;
            line-height: 30px;
            margin: 10px 0px 30px 0px;
            display: none;
        }
    </style>