javascript - 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>
解决方案
推荐阅读
- asp.net-core - 将 AspNet Core 部署到 GKE 导致连接被拒绝
- google-apps-script - 多次调用 Gmail 操作后出现“不允许 Gmail 操作”异常;如何避免?
- javascript - How to print an attribute with double quotes?
- javascript - 刷新时反应路由器空白页面。页面有道具
- javascript - 如何从 MongoDB 字符串数组中检索主题标签?
- perl - mod_perl2:按模块位置而不是名称使用模块
- java - Java - 创建抽象对象的工厂类?
- r - 使用 R 中的简单特征库识别多边形的公共边界
- vim - VIm - 查找和替换管道字符
- mongodb - 删除 MongoDB 中的记录而不是不再有有效的 URL