Problems with using document.write ()

Our support will answer all your general questions here.

Moderator: Rock

Post Reply
lucius
Posts: 3
Joined: Thu Jul 14, 2016 11:16 pm

Problems with using document.write ()

Post by lucius »

Hi, apparently Google doesn't like the use of document.write () because it makes mobile pages slower, I've been reading a lot on the subject, the recommendation is to replace that part of the code ... In the Google Chrome console appears a violation on one of my sites for using document.write () (this is used by the incomig traffic code), I need to solve it.

I wrote all this to one of his boys through ICQ, he asked me for more details and I gave them to him, but two weeks have passed and he doesn't answer me, that's why I had to open a publication here.

This is a part of what the console say:

(index):109 [Violation] Avoid using document.write(). https://developers.google.com/web/updat ... ment-write
rt_js_code @ (index):109
(anonymous) @ (index):111
[Violation] Forced reflow while executing JavaScript took 44ms.

I cannot use the standard code ( include php) because my site uses a cache system and incoming traffic is not counted, I must necessarily use its javascript code. Any suggestion would be welcome.
texpert
Site Admin
Posts: 719
Joined: Sat Mar 14, 2009 5:54 pm

Re: Problems with using document.write ()

Post by texpert »

Hello,
can you try this code instead of document.write(); just comment out document.write(with the code); and add this bellow:

var img = document.createElement('img');
img.setAttribute('src', 'http://'+domain+'in.php?p=default&ref='+ref+'&rnd='+rnd+'&rImg=1&refremote='+docref);
img.setAttribute('width', '1');
img.setAttribute('height', '1');
img.setAttribute('border', '0');

var elem = document.createElement('div');
elem.style.cssText = 'display: none;';
elem.appendChild(img);
document.body.appendChild(elem);


Let me know if it's working.

Best regards,
Alex
lucius
Posts: 3
Joined: Thu Jul 14, 2016 11:16 pm

Re: Problems with using document.write ()

Post by lucius »

Thanks for replying, I have updated the code with the information you provided me and all the warnings and errors have disappeared from the Google Chrome console, apparently everything is fine, but I will wait a couple of hours to check if this new code tracks incoming visits, I'll keep you informed.
lucius
Posts: 3
Joined: Thu Jul 14, 2016 11:16 pm

Re: Problems with using document.write ()

Post by lucius »

The new code tracks visits, apparently everything is fine, the problem is solved.
Post Reply