How can I make sure a javascript cookie is set before page load? 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?
The solution in this case was to set a path on the jquery.cookie

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