日期:2014-05-16  浏览次数:20412 次

js中使用document输出信息时,总是有进度条

?

关于document.write()方法

今天js中使用document输出信息时,总是有进度条,在网上找到了这篇文章,很有帮助

原来的代码:

<html>
??? <body>
??????? <script>???????????
??????????? var h = ‘<html><body>aaaaaaaaaaaa</body></html>’;

//注意这里 document.write();方法对字符串“<script>””</script>””</html>””</body>”极可能出现匹配或过滤错误,导致输出不正常,最好转义一下。
??????????? h = h.replace("</","</");
??????????? function a ()
??????????? {???????????????
??????????????? document.write(h);

?????? return false;
??????????? }
??????? </script>
??????? <input type="button"? onclick ="a()" value = "Try me"/>
??? </body>???
</html>

在firefox下,显示正常,但是进度条总显示正在加载。

后来发现是我忘了关闭document;

写到当前的文档

<html>

<head>
<title>write example</title>

<script type="text/javascript">

function newContent()
{
alert("load new content");
document.open();
document.write("<h1>Out with the old - in with the new!</h1>");
document.close();
}

</script>
</head>

<body onload="newContent();">
<p>Some original document content.</p>
</body>
</html>

写的新打开的文档,或者 iframe

<html>

<head>

<title>Write to IFRAME </title>

</head>

<body>

<iframe width="50%" height="100" id="iframe1" src=""></iframe>

<script language="JavaScript">

<!–

var str = ‘Hello I am sending this to <b>IFRAME</b>’;

// NO IE

if (!document.all) {

????? var ifr1 = document.getElementById(‘iframe1′);

????? ifr1.contentWindow.document.open();

????? ifr1.contentWindow.document.write(str);

????? ifr1.contentWindow.document.close();

}

// IE

else {

document.frames[0].document.open();

document.frames[0].document.write(str);

document.frames[0].document.close();

}

//–>

</script>

</body>

</html>

?

这里还有个比较复杂的例子等等研究一下

<html>

<head>

<title>Write to IFRAME </title>

</head>

<body>

<script language="javascript">

var txt="";

function handleErr(msg,url,l)

{

txt="There was an error on this page.nn"

txt+="Error: " + msg + "n"

txt+="URL: " + url + "n"

txt+="Line: " + l + "nn"

txt+="Click OK to continue.nn"

alert(txt)

return true

}

onerror=handleErr

function loadExternal(url) {

? var ifr1 = document.getElementById(‘srcFrame’);

? if (ifr1) {

????? ifr1.contentWindow.document.location = url;

??? //window.frames['srcFrame'].location? = url;

??? return false;

? }

? return true;

}

function GetInnerHTML () {

????? var str = null;

????? if (!document.all) {

??????????? var ifr1 = document.getElementById(‘srcFrame’);

????????????????? str = ifr1.contentWindow.document.documentElement.innerHTML;

????? }

????? else {

????????????????? str = window.frames['srcFrame'].document.body.innerHTML;????????????
????? }

????? return (str);

}

function displayExternal() {

????? var str = GetInnerHTML ();

????? if (!str) { alert (‘Could not read contents of source iframe’); return; }

????? if (!document.all) {

??????????? var ifr1 = document.getElementById(‘trgFrame’);

??????????? ifr