Page 1 of 1

Problems with using document.write ()

Posted: Mon Apr 13, 2020 9:44 pm
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.

Re: Problems with using document.write ()

Posted: Wed Apr 15, 2020 2:47 pm
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

Re: Problems with using document.write ()

Posted: Wed Apr 15, 2020 3:55 pm
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.

Re: Problems with using document.write ()

Posted: Thu Apr 16, 2020 4:56 pm
by lucius
The new code tracks visits, apparently everything is fine, the problem is solved.