在什么情况下 document.open() 会返回 null?
我试图了解在 Internet Explorer 6 和 Windows XP 下运行的 JavaScript 密集型瘦客户端应用程序中看到的间歇性脚本错误。问题的根本原因是以下函数调用返回 null
值(但它确实成功且没有错误):
var doc = targetWindow.document.open("text/html","_replace");
其中 targetWindow
是 窗口 对象。
targetWindow
和 targetWindow.document
都不是 null
,所以我很难理解为什么这个调用会返回 null
。我对文档的解释是此方法不应返回 null。
这段代码多年来一直没有改变并且完美地工作 - 直到我明白为什么会发生这种情况之前,我不确定我该如何处理这个问题,或者可能发生了什么变化导致这种情况开始发生。
什么可能导致此函数调用返回 null?
I'm trying to understand an intermittent script error that I am seeing in a JavaScript intensive thin-client application running under Internet Explorer 6 and Windows XP. The root cause of the problem is that the following function call returns a null
value (however it does succeed without an error):
var doc = targetWindow.document.open("text/html","_replace");
Where targetWindow
is a window object.
Neither targetWindow
nor targetWindow.document
is null
and so I'm struggling to understand why this call would return null
. My interpretation of the documentation is that this method shouldn't ever return null.
This code has been unchanged and working perfectly for many years - until I understand why this is happening I'm not sure either how I might handle this, or what might have changed to cause this to start happening.
What might cause this function call to return null?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据文档,您应该传递“replace”,而不是“_replace”。试试这个:
既然你说你的代码已经工作了很多年,那么很可能有些东西已经改变了,上面的建议可能不是问题。不过,它仍然值得一试。
您最近是否更改了应用程序中使用的任何 js 文件/库?另外,您在页面中使用任何浏览器插件吗?其中任何一个的较新版本可能会以某种方式影响您对“document.open”的调用。
According to the documentation you should be passing "replace", not "_replace". Try this instead:
Since you say your code has worked for years, then it is likely that something has changed and the above suggestion may not be the issue. However, it is still worth a try.
Have you changed any js files / libraries you are using in your application lately? Also, are you using any browser plugins within the page? It is possible that a newer version of either of these could be somehow affecting your call to "document.open".
按照 W3C 标准,document.open() 没有任何参数。查看此链接:http://www. w3.org/TR/DOM-Level-2-HTML/html.html#ID-72161170
我建议您使用 W3C 文档而不是 Microsoft 的文档,因为使用 W3C 您确信它适用于所有现代浏览器,而微软以添加扩展而闻名,当然,这些扩展仅适用于他们自己的产品。它被称为 EEE(拥抱、延伸和熄灭)。
只需使用不带参数的 document.open() 即可。有一些方法可以操纵用户历史记录,但这被称为不良编程实践。历史记录是用户的私人数据,Web 应用程序不应尝试操纵它。
document.open() does not have any parameters by W3C standard. Check out this link: http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-72161170
I recommend you to use W3C documentation instead of Microsoft's one because with W3C you are sure it works on all modern browsers, while Microsoft is well known for adding extensions that, of course, works only in their own products. It's called EEE (Embrace, extend and extinguish).
Simply use document.open() without arguments. There are ways to manipulate user history, but that's called bad programming practice. History is user's private data and web application should not try to manipulate it.