当前位置: 首页 > 图文教程 > 网络编程 > Javascript > document.open() 与 document.write()的区别

Javascript
Javascript常用运算符(Operators)-javascript基础教程
JS是否可以跨文件同时控制多个iframe页面的应用技巧
不用ajax实现点击文字即可编辑的方法
用javascript来实现动画导航效果的代码
javascript支持firefox,ie7页面布局拖拽效果代码
IE与Firefox下javascript getyear年份的兼容性写法
javascript 不让鼠标事件触发
一个简单横向javascript日期控件
使Ext的Template可以解析二层的json数据的方法
表单项的name命名为submit、reset引起的问题
可以显示单图片,多图片ajax请求的ThickBox3.1类下载
两个DIV等高的JS的实现代码
支持IE和firefox的js代码美化加亮源码
2007/12/23更新创意无限,简单实用(javascript log)
DOMAssitant最新版 DOMAssistant 2.5发布
用js实现的页面关键字密度查询代码
Javascript函数加壳多用于事件绑定
收集的几个不错的javascript类小例子
javascript下function声明一些小结
JS的replace方法与正则表达式结合应用讲解

Javascript 中的 document.open() 与 document.write()的区别


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 113 ::
收藏到网摘: n/a

document.open() 打开一个新的空白文档,在IE下,open有两个默认参数,相当于document.open("text/html",'""),第二个参数只有一个值可选:replace,如果启用了该值,则新建的文档会覆盖当前页面的文档(相当于清空了原文档里的所有元素,且不能后退即,浏览器的后退按钮不可用);
看一个例子:
<SCRIPT LANGUAGE="JavaScript">
<!--
function test(){
document.open("text/html","replace");
document.writeln(Math.random());
document.write("<input type='button' value='back(第二个按钮)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第三个按钮)' onclick='history.back()'>")
document.close();
document.open("text/html","");
document.writeln(Math.random());
document.write("<input type='button' value='back(第四个按钮)' onclick='history.back()'>")
document.close();
}
//-->
</SCRIPT>
<input type="button" value="第一个按钮" onclick="test()">
平常都不写document.open() 与 document.close(),因为浏览器会在write之前先open一个文档,再把write的内容输出到原文档里面。write结束后,默认是不会有close的,否则第二行document.write的时候就会覆盖之前的write。