2

I need to wait to run a function until I have a cookie. The setting of the cookie is out of my hands. I'm using the jQuery cookie plugin to retrieve the cookie.

I think the cookie is set after the following is executed:

$.ajax({
    url: '//munchkin.marketo.net/munchkin.js',
    dataType: 'script',
    cache: true,
    success: function() {
        Munchkin.init('XXX-XXX-XXX');
    }
})

On server A, it was enough to place the function call inside the success parameter.

On server B, the cookie is undefined at the time that the "success" function is executed. I also tried the following on server B:

var post = $.ajax({
    url: '//munchkin.marketo.net/munchkin.js',
    dataType: 'script',
    cache: true,
    success: function() {
        Munchkin.init('XXX-XXX-XXX');
    }
});
post.done(function(p){
    makeLeadCall($.cookie('_mkto_trk'));
});

How can I wait until the cookie has a value before executing the function?

Keyslinger
  • 4,903
  • 7
  • 42
  • 50
  • are you sure the cookie is being set ever on B ? – dandavis Mar 07 '15 at 01:39
  • 1
    I think the question we need to ask is "what exactly are you trying to do"? A cookie is a piece of data returned with an HTTP request - to be sent back to the server on subsequent requests - it's a way for a server to persist data on the client It is NOT a tool for passing information to the client for the client's own use - thus when it's "set" isn't something which will be rigidly defined (it just has to be returned) You may also find using some sort of debug (Devtools->Network) or Proxy/Sniffer (Fiddler) will help you determine IF the cookie is even being sent... –  Mar 07 '15 at 02:01

0 Answers0