- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在测试一些 JavaScript 代码,并意识到这段代码......
var msg = "Hello, World!";
document.open();
document.write(msg);
document.close();
...与这个结果相同:
var msg = "Hello, World!";
document.write(msg);
有什么区别?
最佳答案
围绕 document.write 的许多行为是在正式规范之前建立的,因此行为在浏览器中是不一致的并且有些随意。但是,行为现在相当一致,但根据调用时间的不同会有所不同。
不鼓励使用document.write,但它仍然有用。它无处不在的支持意味着如果确实需要适应旧的浏览器,它可以用作其他技术的替代品。
如果您在加载文档时使用 document.write(例如文档源中的脚本元素),则无需调用打开或关闭。导航到时打开文档,内容加载完成时关闭(即 load 事件发生时)。因此,只要在加载 发生之前执行所有写入语句,浏览器就会完成剩下的工作。
一旦文档完成加载(例如 load 事件已被分派(dispatch)),那么对 document.write 的调用将首先调用 clear 这将清除文档的全部 内容,所有内容。在这种情况下,并非所有浏览器都会在对 write 的调用结束时自动调用 close。
一些浏览器会进行猜测并似乎在稍后调用关闭(IE?),其他浏览器(Firefox、Safari)将保持文档打开,这可能会导致一些异常行为。 p>
如果你打开一个子窗口,例如使用 window.open,然后从父级写入它,写入将在页面加载完成后发生,因此它将清除文档。例如
function openWindow() {
var win = window.open('http://www.google.com','','')
win.document.write(
'<!doctype html><title>foo</title><div>Here is a div</div>'
)
win.document.close();
}
在这种情况下,您永远不会看到 Google,调用 write 会在页面加载并写入新内容时清除页面。
此外,浏览器不会自动调用关闭,您可以对document.write 进行后续调用,它们将附加到现有标记,例如
// Global
var win;
// Open new window and write to it, don't close
function openWindow() {
win = window.open('http://www.google.com','','')
win.document.write(
'<!doctype html><title>foo</title><div>Here is a div</div>'
)
}
// Call some time later, content will be appended
function writeToWindow(){
win.document.write(
'<div>Here is the second div</div>'
)
}
您可能会在选项卡或窗口中看到一些动画,表明它仍在加载。
如果在上面,openWindow 在结束之前调用了 document.close,那么随后对 writeToWindow 的调用将首先清除文档,因此div 是文档中的唯一元素(以及由浏览器自动添加的强制性 HTML、HEAD 和 BODY 元素,并且可能是通过纠错添加的 TITLE)。
因此,在这种情况下,您应该在适当的时候调用close。
如果在加载期间调用以下内容,则:
var msg = "Hello, World!";
// The following has no effect as the document is currently
// loading, therefore already open
document.open();
// Writes to the document
document.write(msg);
// The following has no effect because the window hasn't fired load yet
document.close();
所以只有 document.write 行在这种情况下有用。
部分播放代码:
var win;
function openWindow() {
win = window.open('http://www.google.com','','')
win.document.write(
'<!doctype html><title>foo</title><div>Here is a div</div>'
)
win.document.close();
}
function writeToWindow(){
win.document.write(
'<div>Here is the second div</div>'
)
}
关于javascript - document.open 和 document.close 是必要的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27854494/
我正在尝试计算 iFrame 的高度,但不明白为什么 document.body.offsetHeight + document.body.bottomMargin 不等于 document.docu
我正在使用 Node/Mongoose/MongoDB 并尝试构建一个轮询应用程序。一个关键需求是跟踪单个用户对同一民意调查的响应如何随时间变化(他们一遍又一遍地进行同一民意调查)。 我有一个用户模型
首先,我不是普通的博主,我很困惑。如果我的问题不符合要求,请指导我。我会努力改进的。 我已提交 Microsoft Code Review 的 Microsoft CRM 插件。我是 JavaScri
谁能解释为什么使用类似的东西: gci -force "\\computername\c$\users\username\Documents" -recurse 或者 gci -force "\\co
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, over
这个问题已经有答案了: What is the (function() { } )() construct in JavaScript? (28 个回答) 已关闭 6 年前。 说实话,一开始我以为我可
document.getElementsByTagName("*") 适用于 IE/Firefox/Opera,但不适用于 Chrome 和 Safari。 document.all 适用于 IE/C
这个问题在这里已经有了答案: What is the difference between Document and document in JavaScript? (2 个答案) 关闭 8 年前。
我以某种方式发现将事件监听器添加到文档的行为有点奇怪。虽然向 HTMLElements 添加监听器工作正常,但向文档添加监听器不起作用。但奇怪的是,使用 jQuery 可以让它工作。 那么有人可以解释
谁能告诉我这两个 JavaScript 命令之间的区别? 这两个跨主要浏览器的兼容性是什么?我知道 documentElement 与大多数浏览器兼容。 谢谢 最佳答案 document.docume
什么时候应该使用 document.all 与 document.getElementById? 最佳答案 document.all 是 Microsoft 对 W3C 标准的专有扩展。 getEle
当升级到 react-native 0.61.2 时,这个问题出现了。我做到了从手机中删除了 apk 和自动链接使用 react-native link 然后 react-native run-and
当升级到 react-native 0.61.2 时,这个问题出现了。我做到了从手机中删除了 apk 和自动链接使用 react-native link 然后 react-native run-and
我将收到 tungstenite::Message ,它将包含来自客户端的bson文档。我可以将tungstenite::Message转换为Vec,但是如何在服务器端将其转换回 bson::docu
我这里有一个简单的疑问: 文档对象范围位于浏览器选项卡内:我的意思是如果我设置document.tab1 ='tab1' 在一个浏览器选项卡中 它在其他选项卡中不可用。 但是 document.coo
我经常使用并看到推荐的 dom 访问结构,例如这样动态地将内容添加到页面: loader = document.createElement('script'); loader.src = "myurl
我对 JQuery 还很陌生。我正在使用this JQuery 函数在元素上显示工具提示。 我根据我的需要(在这个社区的帮助下)以这种方式编辑了代码: $(document).ready(functi
我想知道哪个是运行js代码的正确方法,该代码根据窗口高度计算垂直菜单的高度并按时设置,不晚不早。 我正在使用 document.ready 但它并没有真正帮助我解决这个问题,它有时没有设置,我必须重新
我正在浏览一个 js 文件并发现这个声明var dataobj=document.all? document.all.id_name : document.getElementById("id_nam
想知道何时使用,这适用于什么浏览器? if (document.all&&document.getElementById) { // Some code block } 最佳答案 我认为没有任何重要的
我是一名优秀的程序员,十分优秀!