Using cookiebot in auto blocking mode prevents document.write in following scripts

Comments

3 comments

  • Hi Johannes,

    First of all I would like to suggest you to avoid using document.write because of several reasons. 
    For users on slow connections, such as 2G, 3G or even slow Wi-Fi, loading external scripts or other resources dynamically injected via document.write() can delay the display of main page content by tens of seconds (which is a lot) because the web browser has to pause the HTML parsing. 
    Another issue to be considered about using document.write(): if the DOM tree has already been built, the use of document.write will force the browser to build it again which could mess with the overall website performance. 
    There is also other well known issue that could be caused by using document.write() and if you would like you could make your own research in the Internet about this.

    However, the better method we suggest would be to do the following which can replace the document.write
    <script type="texy/javascript">
    (function(d, l) {
    link = d.createElement(l);
    link.src = "foo.css";
    d.appendChild(link);
    })(document, 'link');
    </script>
    This is also working fine with the auto blocking feature.

    Regards,
    Spas
    0
    Comment actions Permalink
  • Avatar
    Johannes Ulfkjær Jensen

    Hi Spas,

    Thanks for the tips but I am well aware of the tradeoffs for using document.write().

    The snippet you give (in which I assume you mean d.head.appendChild) is not equivalent nor sufficient as it will not block the rendering which in turn leads to a flash of unstyled content of the entire page which is completely unacceptable.

    The use of document.write is perfectly valid api despite its drawbacks.

    Is the lack of support for this final or would you consider it a bug?

    Kind regards

    0
    Comment actions Permalink
  • Hi Johannes,

    You are right that document.write() is a valid Javascript, but it just doesn't work well with auto blocking Cookiebot feature. Because of the fact that document.write will be delayed until the very end and when everything else is already rendered and build . So basically any calls to that file before that will fail.
    In case that you would like to use document.write() you could of course assign the data-cookieconsent="ignore" attribute to the script, so it gets executed immediately.

    Regards,
    Spas
    0
    Comment actions Permalink

Please sign in to leave a comment.