What are alternatives to [removed]?

后端 未结 10 2009
北荒
北荒 2020-11-22 05:11

In tutorials I\'ve learnt to use document.write. Now I understand that by many this is frowned upon. I\'ve tried print(), but then it literally sen

相关标签:
10条回答
  • 2020-11-22 05:37

    Just dropping a note here to say that, although using document.write is highly frowned upon due to performance concerns (synchronous DOM injection and evaluation), there is also no actual 1:1 alternative if you are using document.write to inject script tags on demand.

    There are a lot of great ways to avoid having to do this (e.g. script loaders like RequireJS that manage your dependency chains) but they are more invasive and so are best used throughout the site/application.

    0 讨论(0)
  • 2020-11-22 05:37

    Try to use getElementById() or getElementsByName() to access a specific element and then to use innerHTML property:

    <html>
        <body>
            <div id="myDiv1"></div>
            <div id="myDiv2"></div>
        </body>
    
        <script type="text/javascript">
            var myDiv1 = document.getElementById("myDiv1");
            var myDiv2 = document.getElementById("myDiv2");
    
            myDiv1.innerHTML = "<b>Content of 1st DIV</b>";
            myDiv2.innerHTML = "<i>Content of second DIV element</i>";
        </script>
    </html>
    
    0 讨论(0)
  • 2020-11-22 05:38

    I'm not sure if this will work exactly, but I thought of

    var docwrite = function(doc) {               
        document.write(doc);          
    };
    

    This solved the problem with the error messages for me.

    0 讨论(0)
  • 2020-11-22 05:39

    I fail to see the problem with document.write. If you are using it before the onload event fires, as you presumably are, to build elements from structured data for instance, it is the appropriate tool to use. There is no performance advantage to using insertAdjacentHTML or explicitly adding nodes to the DOM after it has been built. I just tested it three different ways with an old script I once used to schedule incoming modem calls for a 24/7 service on a bank of 4 modems.

    By the time it is finished this script creates over 3000 DOM nodes, mostly table cells. On a 7 year old PC running Firefox on Vista, this little exercise takes less than 2 seconds using document.write from a local 12kb source file and three 1px GIFs which are re-used about 2000 times. The page just pops into existence fully formed, ready to handle events.

    Using insertAdjacentHTML is not a direct substitute as the browser closes tags which the script requires remain open, and takes twice as long to ultimately create a mangled page. Writing all the pieces to a string and then passing it to insertAdjacentHTML takes even longer, but at least you get the page as designed. Other options (like manually re-building the DOM one node at a time) are so ridiculous that I'm not even going there.

    Sometimes document.write is the thing to use. The fact that it is one of the oldest methods in JavaScript is not a point against it, but a point in its favor - it is highly optimized code which does exactly what it was intended to do and has been doing since its inception.

    It's nice to know that there are alternative post-load methods available, but it must be understood that these are intended for a different purpose entirely; namely modifying the DOM after it has been created and memory allocated to it. It is inherently more resource-intensive to use these methods if your script is intended to write the HTML from which the browser creates the DOM in the first place.

    Just write it and let the browser and interpreter do the work. That's what they are there for.

    PS: I just tested using an onload param in the body tag and even at this point the document is still open and document.write() functions as intended. Also, there is no perceivable performance difference between the various methods in the latest version of Firefox. Of course there is a ton of caching probably going on somewhere in the hardware/software stack, but that's the point really - let the machine do the work. It may make a difference on a cheap smartphone though. Cheers!

    0 讨论(0)
提交回复
热议问题