eu-cookie-law.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. ( function( $ ) {
  2. var cookieValue = document.cookie.replace( /(?:(?:^|.*;\s*)eucookielaw\s*\=\s*([^;]*).*$)|^.*$/, '$1' ),
  3. overlay = $( '#eu-cookie-law' ),
  4. initialScrollPosition,
  5. scrollFunction;
  6. if ( overlay.hasClass( 'top' ) ) {
  7. $( '.widget_eu_cookie_law_widget' ).addClass( 'top' );
  8. }
  9. if ( overlay.hasClass( 'ads-active' ) ) {
  10. var adsCookieValue = document.cookie.replace( /(?:(?:^|.*;\s*)personalized-ads-consent\s*\=\s*([^;]*).*$)|^.*$/, '$1' );
  11. if ( '' !== cookieValue && '' !== adsCookieValue ) {
  12. overlay.remove();
  13. }
  14. } else if ( '' !== cookieValue ) {
  15. overlay.remove();
  16. }
  17. $( '.widget_eu_cookie_law_widget' ).appendTo( 'body' ).fadeIn();
  18. overlay.find( 'form' ).on( 'submit', accept );
  19. if ( overlay.hasClass( 'hide-on-scroll' ) ) {
  20. initialScrollPosition = $( window ).scrollTop();
  21. scrollFunction = function() {
  22. if ( Math.abs( $( window ).scrollTop() - initialScrollPosition ) > 50 ) {
  23. accept();
  24. }
  25. };
  26. $( window ).on( 'scroll', scrollFunction );
  27. } else if ( overlay.hasClass( 'hide-on-time' ) ) {
  28. setTimeout( accept, overlay.data( 'hide-timeout' ) * 1000 );
  29. }
  30. var accepted = false;
  31. function accept( event ) {
  32. if ( accepted ) {
  33. return;
  34. }
  35. accepted = true;
  36. if ( event && event.preventDefault ) {
  37. event.preventDefault();
  38. }
  39. if ( overlay.hasClass( 'hide-on-scroll' ) ) {
  40. $( window ).off( 'scroll', scrollFunction );
  41. }
  42. var expireTime = new Date();
  43. expireTime.setTime( expireTime.getTime() + ( overlay.data( 'consent-expiration' ) * 24 * 60 * 60 * 1000 ) );
  44. document.cookie = 'eucookielaw=' + expireTime.getTime() + ';path=/;expires=' + expireTime.toGMTString();
  45. if ( overlay.hasClass( 'ads-active' ) && overlay.hasClass( 'hide-on-button' ) ) {
  46. document.cookie = 'personalized-ads-consent=' + expireTime.getTime() + ';path=/;expires=' + expireTime.toGMTString();
  47. }
  48. overlay.fadeOut( 400, function() {
  49. overlay.remove();
  50. } );
  51. }
  52. } )( jQuery );