0

I've got some tracking code that I have to embed in the footer of my page. The tracking code is from a 3rd party company and uses document.write which of course is blocking. I've sent a support request to the company and they don't seem to have any interest in changing the code.

The tracking code looks something like this:

document.write (
  '<img src="http://company-url.com/visitor.gif?ts='+
  new Date().getTime()+
  '&ref='+escape(document.referrer) + '">'
);

So, my question is, is it possible to make document.write non blocking?

4
  • What do you mean it is blocking? Can you not just rewrite it to be document.getElementById('footer').innerHTML = '<img ... />'? Commented Dec 9, 2013 at 16:51
  • 1
    I wouldn't use document.write at all if I had the choice! Commented Dec 9, 2013 at 16:51
  • So you know, it's not document.write that's blocking, but rather all script execution is blocking, unless you use stuff like defer or async. But then document.write doesn't work any longer. Commented Dec 9, 2013 at 16:52
  • Is this third-party code an inline script or one with an external src? If the latter, there's not much you can do, but if it's inline so you can change it, @bfavaretto's solution would work. Commented Dec 9, 2013 at 16:58

1 Answer 1

1

No, it's not possible to make document.write asynchronous. But you can just append an img to the body after page load:

window.onload = function() {
    var url = 'http://company-url.com/visitor.gif?ts='+new Date().getTime()+'&ref='+escape(document.referrer);
    var img = document.createElement('img');
    img.src = url;
    document.body.appendChild(img);
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.