flapane
flapane

Reputation: 543

jquery: check if window has been scrolled once and do something

Because of a new implementation of the cookie law, I have to gather consent for some cookies. One of the methods, is to wait for the user to scroll the page once, which has been legally meant to be an implicit consent.

I tried with:

jQuery(window).one('scroll', function(){
  $(this).data('scrolled', true);
});

if(jQuery(window).data('scrolled')) {
    console.log( 'ok' );
    //place cookies
} else {
    console.log( 'no' );
}

inside a script ( http://www.primebox.co.uk/projects/jquery-cookiebar/ ) that is called via:

$(document).ready(function(){
                $.cookieBar({
                }); 
            });

However, console.log always tells me "no", regardless of the scrolling of the page.

Any hints? Thanks in advance

Upvotes: 0

Views: 206

Answers (2)

Zohaib Waqar
Zohaib Waqar

Reputation: 1239

  1. Check scroll and do some thing you want

    window.onscroll = function (e) {
    // called when the window is scrolled.
    }

This will call every time when user scroll if you want to call it only once then do some trict

  1. Make a hidden Field

  2. Now set value of this hidden field to true when scroll method call

    window.onscroll = function (e) { if (document.getElementById("ScrollOnceCheck").value == "false") { document.getElementById("ScrollOnceCheck").value = "true"; // update cookie } }

Upvotes: 1

Rayon
Rayon

Reputation: 36609

Your code works fine if I change this like this:

<script>
    jQuery(window).one('scroll', function()
    {
        $(this).data('scrolled', true);
        if(jQuery(window).data('scrolled'))
        {
            console.log('ok');
            //place cookies
        }
        else
        {
            console.log('no');
        }
    });
</script>

However I could not make out what $.cookieBar is doing ?

Upvotes: 1

Related Questions