I am setting a simple javascript cookie and checking it before firing a jquery ui dialog

(function ($) {
  Drupal.behaviors.setPopupConfig = {
   attach: function (context, settings) {
   if ($.cookie('modalServed') == null) {
    $.cookie('modalServed', 'yes');
    // build and show the dialog

I am adding the javascript with drupal_add_js in a hook_entity_load() in a module. In testing I've noticed that if I have a link element in the modal with a destination set to another place in the site with a potential modal dialog, the second dialog will fire before the cookie is set in the browser, which is not the intended behavior. I've tried adding weight and scope options to the drupal_add_js call, but it doesn't solve the problem. Is there another way to make sure the modal js only fires after the cookie is set?

  • I hope you are doing this after you got the GDPR consent. ;)
    – leymannx
    Commented Oct 14, 2018 at 9:33
  • This particular site was dead before GDPR implementation, the question is quite old, but this is a valid comment moving forward.
    – Steve
    Commented Oct 15, 2018 at 17:25

1 Answer 1


The solution in this case was to set a path on the jquery.cookie

$.cookie('modalServed', 'yes', {path: '/'});

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.