0

I'm getting "[Violation] Avoid using document.write()." in Google Dev tools. I inherited the code and am not sure how to correct or update it. It is on two lines:

function include(scriptUrl){document.write('<script src="'+scriptUrl+'"></script>');}

and

document.write('<meta name="viewport" content="width=device-width,initial-scale=1.0'+userScale+'">');

I've read the answers on this forum, but there are several "includes" of jquery files within subsequent functions, and I can't target where "scriptURL" is being populated. The final output is list of js files. Here is an example function that supplies this "scriptURL" variable:

(function($) {
    var o = $('.thumb');
    if (o.length > 0) {
        include('js/jquery.fancybox.js');
        include('js/jquery.fancybox-media.js');
        $(document).ready(function() {
            o.fancybox();
        });
    }
})(jQuery);;

where the "js/jquery.fancybox.js" and "js/jquery.fancybox-media.js" files are output as

<script src="js/jquery.fancybox.js"></script>
<script src="js/jquery.fancybox-media.js"></script>

above the body tag.

The 'meta name="viewport' function above follows this function:

var ua = navigator.userAgent.toLocaleLowerCase(),
    regV = /ipod|ipad|iphone/gi,
    result = ua.match(regV),
    userScale = "";
if (!result) {
    userScale = ",user-scalable=0"
}

Any help is appreciated.

  • 1
    Possible duplicate of [What are alternatives to document.write?](https://stackoverflow.com/questions/4537963/what-are-alternatives-to-document-write) – Calvin Nunes Nov 01 '18 at 17:17
  • Consider JavaScript's [`appendChild()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild) or jQuery's [`append()`](http://api.jquery.com/append/). – showdev Nov 01 '18 at 17:18

0 Answers0