title: Get cookie on first load
body: <p>So I created this new thread for this one since it was so bad explained, but was told to edit this one:</p>
  
  <p><strong>New Text</strong></p>
  
  <p>I need to receive a user_id from a external website. I only need to receive this if the user come to site A from site B. So when he does that I then save that user_id in a cookie, that part is working fine. But I also need to get the value of the cookie when he first land on the page, so let me try to explain how I have done this:</p>
  
  <p>First I look at if user is coming from another domain:</p>
  
  <pre><code>var ownDomain = location.hostname,
      referrerDomain = document.referrer.split('/')[2],
      author = '';
  
  if ( ownDomain != referrerDomain) {
    var script = document.createElement('script');
    script.src = '//javascript.mamp/json/?callback=setAuthorCookie';
    document.getElementsByTagName('head')[0].appendChild(script);
  }
  </code></pre>
  
  <p>If he does that, then we insert a javascript which calls the callback <code>setAuthorCookie()</code>, which looks like this:</p>
  
  <pre><code>function setAuthorCookie( data ) {
    var exdate = new Date(),
        exdays = 1,
        value = data['userId'],
        cookie = 'partnerOptimizerId',
        exdate = new Date();
  
     author = value;
  
     exdate.setDate(exdate.getDate() + exdays);
     var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
     document.cookie=cookie + "=" + c_value;
  }
  </code></pre>
  
  <p>Now as you can see I set the author value inside that function,  so I should be able to console.log out the value of author and get the correct value:</p>
  
  <pre><code>console.log( author )
  </code></pre>
  
  <p>I return nothing, which is kind of where Im stuck right now. I guess it is because that some how <code>setAutorCookie</code> runs after I <code>console.log( author )</code>, but I was hoping someone could help me on this, is it because <code>author</code> is a local variable in <code>setAuthorCookie</code> ?</p>
  
  <p><strong>After some testing I can see that setAuthorCookie runs after the last console.log() and that is why it is empty but can somebody help with how I change that ?</strong></p>