Loading jQuery using document.write

If you load jQuery using document.write, the jQuery will not be loaded immediately.  You can use setTimeout to set a brief delay before using it.

Here I set a brief delay of 0.1s as in line 17.

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
            (function () {
                if (window.jQuery === undefined)
                {
                    document.write('<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"><\/script>');
                }
                else
                {
                    jQuery = window.jQuery;
                }

                setTimeout(function () {
                    $('#demo').html('Document is ready and loaded');
                }, 100);

            }());
        </script>
    </head>
    <body>
        <h4 id='demo'>Document Not Ready</h4>
    </body>
</html>

Try The Code

 

Leave a comment

Leave a Reply