我可以使用 Javascript 检测 XHTML 解析错误吗?
当我的 XHTML 页面中出现标记错误时,Mozilla Firefox 会显示“死机黄屏”,在黄色背景上仅显示一条大的红色错误消息。
虽然这些错误很少见,但它们对用户来说非常不友好。
有没有办法可以使用 Javascript 检测这些,从而将消息发送回服务器?
到目前为止我发现了什么: - 解析错误之前放置的脚本仍然运行。 (当然。) - 在这些脚本中设置的超时和间隔在解析错误后仍将执行。 - 在 Firefox 中,DOM 是一个
,其中包含
。 如果我查询 document.firstChild.tagName
,我可以检测到这一点。
剩余问题: - 我可以监听哪些事件来检测这种情况的发生? (投票很糟糕。) - 我如何在其他浏览器中检测到这一点?
When I have a markup error in my XHTML page, Mozilla Firefox displays the "Yellow Screen of Death", showing only a large red error message on a yellow background.
While these errors are rare, they are extremely user-unfriendly.
Is there a way I can detect these using Javascript, and thereby send a message back to the server?
What I've discovered so far:
- Scripts placed before the parsing error still run. (Of course.)
- Timeouts and intervals that were set in these scripts will still execute after the parsing error.
- In Firefox, the DOM is a <parsererror>
with a <sourcetext>
inside it. I can detect this if I query document.firstChild.tagName
.
Remaining questions:
- What events are available that I could listen for to detect this happening? (Polling sucks.)
- How can I detect this in other browsers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在客户端捕获解析错误可能是可能的,但它实际上解决了错误的问题。
我知道这不是您所要求的,但除非您正在做一些真正特定于 XHTML 的事情,例如嵌入其他标记语言,否则您应该将页面提供为 text/html 而不是 application/xhtml+xml。 即使它是 XHTML。 通过将其作为 text/html 提供,您将避免遇到的问题,并允许您的页面在 IE 中工作。 请注意,决定使用哪个解析器的是 MIME 类型,而不是 doctype 声明——使用过渡 doctype 无法做到这一点。
也就是说,如果您确实希望将页面解析为 XHTML,那么最好在服务器上处理此类错误。 通过构建 DOM 生成页面,然后发送序列化结果。 如果这不是一个选项,那么首先像现在一样生成页面,但先不要将其传输到客户端。 获取您生成的 XHTML,并使用验证 XHTML 解析器(或者至少是通用 XML 解析器)在服务器端对其进行解析。 如果出现错误,请显示您想要的任何错误页面。 否则,序列化解析后的 DOM 并将其发送到客户端。
综上所述,使用 application/xhtml+xml 的基本规则是:
请记住,XHTML 只是 HTML 4 的重新表述加上嵌入其他语言的能力。 如果您不使用嵌入,您所拥有的是具有不同但几乎完全兼容语法的 HTML 4。 绝大多数 XHTML 文档都以 text/html 形式提供,因此浏览器将其视为 HTML 4。
Catching parse errors on the client might be possible, but it's really solving the wrong problem.
I know this isn't what you asked for, but unless you're doing something truly XHTML-specific like embedding some other markup language, you should serve your page as text/html instead of application/xhtml+xml. Even if it's XHTML. By serving it as text/html you'll avoid the problem you're running into and allow your page to work in IE as well. Note that it's the MIME type and not the doctype declaration that determines which parser is used -- using a transitional doctype won't do it.
That said, if you're really sure you want your page parsed as XHTML, it's better to handle this kind of error on the server. Generate your page by building up a DOM and then send the result of serializing it. If that's not an option, then start by generating the page as you do now but don't transmit it to the client yet. Take the XHTML that you've generated and parse it server-side with a validating XHTML parser (or at the very least, a generic XML parser). If you get errors, display whatever error page you want. Otherwise, serialize the parsed DOM and send that to the client.
In summary, the basic rules for using application/xhtml+xml are:
Remember that XHTML is just a reformulation of HTML 4 plus the ability to embed other languages. If you don't use the embedding, what you have is HTML 4 with a different but almost completely compatible syntax. The overwhelming majority of XHTML documents out there are served as text/html and thus treated like HTML 4 by browsers.
这并不能回答您的问题,但是,为什么不在您的服务器上验证您的 XHTML,在您生成 XHTML 之后/时以及将其发送到浏览器之前呢?
This doesn't answer your question but, instead, why not validate your XHTML on your server, after/when you generate it and before you send it to the browser?
我的第一个问题是:既然 Internet Explorer 不容易让您实际指定 application/xhtml+xml 作为 mime 类型,也不能很好地支持它,为什么您需要检测 XHTML 解析错误?
至于检测错误 - 请查看 http://www.quirksmode.org
My first question would be: Since Internet Explorer does not easily allow you to actually specify application/xhtml+xml as the mime type, nor support it all that well, why do you need to detect XHTML parse errors?
As for detecting the errors - have a look at http://www.quirksmode.org
我建议在服务器端验证文档。 但是,如果您确实想在客户端执行此操作,那么如果正确完成轮询,则没有任何问题(这意味着轮询一定会终止)。
以下应该至少在 Firefox 和 Opera 中工作:
I'd recommend validating the document on the server side. But if you really want to do it on the client side, there's nothing wrong with polling if done correctly (which means that the poll is guaranteed to terminate).
The following should work in at least Firefox and Opera:
我知道这可能不是最有帮助的答案,但是您是否考虑过切换到过渡文档类型?
无论如何,让你的文件通过解析器来检测错误,但要离线进行——冒着向用户显示 YSOD 的风险是不值得的!
I know it's probably not the most helpful answer, but have you considered switching to a transitional doctype?
By all means, put your files through a parser to detect errors, but do it offline - the risk of showing users the YSOD is not worth it!