非致命Javascript错误?
我认为我的脚本中存在某种非致命的bug,它不允许我用Firebug对脚本进行a/调试,b/导致Firefox不断显示一个连接。。。(带着漩涡)当我在页面上的时候。不过脚本似乎运行良好 你知道是什么原因吗非致命Javascript错误?,javascript,syntax-error,Javascript,Syntax Error,我认为我的脚本中存在某种非致命的bug,它不允许我用Firebug对脚本进行a/调试,b/导致Firefox不断显示一个连接。。。(带着漩涡)当我在页面上的时候。不过脚本似乎运行良好 你知道是什么原因吗 <script type="text/javascript"> var xmlHttp; var xmlDoc; loadXMLFile(); function loadXMLFile() { xmlHttp = new window.XMLHttpRequest();
<script type="text/javascript">
var xmlHttp;
var xmlDoc;
loadXMLFile();
function loadXMLFile()
{
xmlHttp = new window.XMLHttpRequest();
xmlHttp.open("GET", "myFile.xml", true);
xmlHttp.onreadystatechange = StateChange;
xmlHttp.send(null);
}
function StateChange()
{
if ( xmlHttp.readyState == 4 )
{
xmlDoc = xmlHttp.responseXML;
processXML();
}
}
function processXML()
{
var firstNames = xmlDoc.querySelectorAll("name");
if (firstNames == null)
{
document.write("Oh poo. Query selector returned null.");
}
else
{
for (var i = 0; i < firstNames.length; i++)
{
document.write(firstNames[i].textContent + "<br>");
}
}
}
</script>
var-xmlHttp;
var-xmlDoc;
loadXMLFile();
函数loadXMLFile()
{
xmlHttp=newwindow.XMLHttpRequest();
open(“GET”,“myFile.xml”,true);
xmlHttp.onreadystatechange=StateChange;
xmlHttp.send(空);
}
函数StateChange()
{
if(xmlHttp.readyState==4)
{
xmlDoc=xmlHttp.responseXML;
processXML();
}
}
函数processXML()
{
var firstNames=xmlDoc.querySelectorAll(“名称”);
if(firstNames==null)
{
write(“Oh poo.Query选择器返回null”);
}
其他的
{
对于(var i=0;i”;
}
}
}
页面中的所有代码都会被解析,但在页面完成之前不会执行。这种情况会发生,因为您是从onreadystatechange
事件处理程序函数调用document.write()
,而不是从解析时间调用
在本例中,document.write()
隐式调用document.open()
,这会清除页面中的所有代码,只剩下document.write()
编写的文本。另外,文档
保持打开状态,这会导致浏览器“忙”。这可以通过使用document.close()
来避免,但它不会阻止原始内容消失
您需要向主体添加一个元素,然后使用一些“真正的”DOM操作方法。大概是这样的:
<div id="qResult"></div>
使用document.write()
是一种不好的形式。在解析页面后使用document.write()
是非常致命的;)。我只是想通过XML在页面上快速打印一些内容。脚本(以及document.write()
)是否在其下面的HTML之前未被解析?该代码位于
内,谢谢!即使对于像document.write()
这样的糟糕形式的函数,了解正在发生的事情也很好。
document.getElementById('qResult').innerHTML = WHAT_EVER_NEEDED