非致命的 Javascript 错误? [英] Non-Fatal Javascript Error?

查看:37
本文介绍了非致命的 Javascript 错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的脚本中存在某种非致命错误,它不允许我使用 Firebug 调试脚本,而 b/导致 Firefox 不断显示连接...(带有漩涡),而我正在在页面上.不过脚本似乎运行良好.

任何可能导致这种情况的想法?

解决方案

页面中的所有代码都被解析,但在页面完成之前不会执行.发生这种情况,因为您是从 onreadystatechange 事件处理函数调用 document.write() 而不是解析时间.

在这种情况下,document.write() 隐式调用了 document.open(),这会清除页面中的所有代码,只剩下由 document.write() 编写的文本.另外 document 保持打开状态,这会导致浏览器忙碌".使用 document.close() 可以避免这种情况,但不会阻止原始内容消失.

您需要向 body 添加一个元素,然后使用一些真实"的 DOM 操作方法.像这样:

然后代替document.write():

document.getElementById('qResult').innerHTML = WHAT_EVER_NEEDED

I think there's some kind of non-fatal bug in my script that's not allowing me to a/ debug the script with Firebug and b/ causes Firefox to constantly display a Connecting... (with the swirly) while I'm on the page. The script seems to run fine though.

Any ideas what could cause that?

<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>

解决方案

All the code in your page is parsed, but not executed before the page is completed. This happens, since you're calling document.write() from onreadystatechange event handler function rather than parsing time.

In this case document.write() implicitly calls document.open(), which wipes all the code out of the page, and only what is left is the text written by document.write(). Also document is left open, which causes the browser being "busy". This can be avoided by using document.close(), but it won't prevent the original content to vanish.

You need to add an element to the body and then use some "real" DOM manipulation method. Something like this:

<div id="qResult"></div>

Then instead of document.write():

document.getElementById('qResult').innerHTML = WHAT_EVER_NEEDED

这篇关于非致命的 Javascript 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆