1

I'm getting crazy over this script. It's not working. Can anyone give me a tip how to fix it?

My code: inside the tag in my html:

HTML Code:

<div class="cookie-message">
    <p class="cookie-content">This website uses cookies. By Browsing this website you acconsent to the use of cookies. </p>
    <p class="cookie-content"><button class="button">OK</button> &nbsp; <a href="http://here-goes-my-cookie-page.com">Read more about cookies</a></p>   
</div>

My CSS file (it's more for styling): Code:

.cookie-message {
      width: 100%;
      background: #333;
      color: #ddd;
      padding: 1em 1em .5em;
      position: fixed;
      left: -5px;
      bottom: 2em;
      text-align: left;
      z-index: 1;
      }

And my javascript is following: Code:

<!--Start Cookie Script--> 
<script>
$('.cookie-header').click(function() {
        $('.cookie-content').slideToggle('fast');
          $('.cookie-content button').click(function() {
                $('.cookie-content').slideUp('fast');
          });
    });
    </script>
     <!--End Cookie Script-->

in fact I'm completely new to JavaScript so I copied it and inserted it before the </body> tag and obviously it is not working...

Can someone help me? I need the message to close when pressing the "ok" button...

1

3 Answers 3

3

You have a mismatch on the class names in the HTML and JQuery selectors.

<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<!-- Stuff -->
    $(document).ready(function(){
        $('.cookie-message').click(function() {
            $('.cookie-content').slideToggle('fast');
              $('.cookie-content button').click(function() {
                    $('.cookie-content').slideUp('fast');
              });
        });
    });
</body>

I believe this is what you're looking for JSFiddle example.

Sign up to request clarification or add additional context in comments.

8 Comments

This is probably correct. The code you are using will look for elements with the class .cookie-message as soon as it can - which may be before any .cookie-message elements exist. By adding the document ready block, the code will wait until the document is finished loading and then start working. Good catch on the mismatched class!
I corrected my code as @SeeSharp suggested, but now i cannot see any message... and in a fiddle I can see that the dialogBox is not dissappearing, just hiding... i would like it to dissappear...
@arcsn Any chance of posting a fiddle? Isn't related to the fact you've already got a cookie set and thus the message isn't showing? You're referencing the JQuery library somewhere in the HTML doc too, right?
i checked my html, and i have a js code for google analytics and also for IE9...should I subscribe to the fiddle site? (I'm new here....)
Nah, just head to jsfiddle.net and enter your HTML, CSS && JS in their relevant boxes. Press 'Save' along the top and copy and paste the URL back here to share.
|
1

You need to wrap the jQuery function with document ready:

$(document).ready(function(){
    $('.cookie-header').click(function() {
        $('.cookie-content').slideToggle('fast');
          $('.cookie-content button').click(function() {
                $('.cookie-content').slideUp('fast');
          });
    });
});

Comments

0

One more important thing about your code is that you're not setting a cookie variable and the user will have to select ok everytime he connects to your website.

This is the piece of code that i use for all my projects

$('#cookie_stop').on('click', function(){//#cookie stop is the Id of the button that closes the disclaimer
    $('#cookie_disclaimer').slideUp(); //#cookie_disclaimer is the Id of the cookie disclaimer container

   //here i set the cookie variable "disclaimer" to true so i can check if the user 
       var nDays = 999; 
       var cookieName = "disclaimer";
       var cookieValue = "true";

       var today = new Date();
       var expire = new Date();
       expire.setTime(today.getTime() + 3600000*24*nDays);
       document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString()+";path=/";
  //done with the cookie
 });

So, when in the HTML where i write the cookie disclaimer part, there's something like this (Php example)

 <?if isset($_COOKIE['disclaimer']){?>
    <!--cookie disclaimer part-->
 <?}?>

10 Comments

Hi I just used your code in my Fiddle... and it's not working... maybe i am doing somthing wrong... (i am completly new to java...)
You have to change the jquery selector based on your code! So, in your case it should be $(.button).on('click', function(){ this part of code mean: When the element with class 'button' is clicked, perform the following actions. (When you button is clicked, close the disclaimer). Then $('.cookie-message').slideUp(); If you link the jsfiddle i will edit it so you can see it in action!
and if it is possible, I would like to make "dissappear" a message after clicking the "ok" button... now it still stays there just minimise, but stays there (and make my design "ugly"). I'm talking about @SeeSharp version.
look at this updated fiddle.. pay attention that to make the cookie disclaimer disappear forever once the user has clicked on ok you will need to use cookies! In the main answer there's an example with php but you can use whatever you want like asp or even javascript i guess jsfiddle.net/aLwpmcqg/7
looks perfect. One question: should I create a separate js file for the js script or include it into my html page? (i want a fast load and i guess it's preferable to separate). and should I leave (or remove) a jquery.min.js script in my <head> tag?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.