首页 > 解决方案 > Use document.ready or not when loading external data from an aJax call?

问题描述

Working on a page to load an external data file via JS and display the contents within PRE tags. Its working, but a friend mentioned I should use always use document.ready() function.

why is it needed?

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv='cache-control' content='no-cache'>
    <meta http-equiv='expires' content='-1'>
    <meta http-equiv='pragma' content='no-cache'>

    <script type="text/javascript">
        function myFunction() {
            var ajaxRq = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
            ajaxRq.open("GET", "data.txt", false);
            ajaxRq.send(null);
            document.write(ajaxRq.responseText);
        };
    </script>
</head>

<body>

<pre><script>myFunction();</script></pre>

</body>
</html>

标签: javascripthtmlajaxperformance

解决方案


推荐阅读