document.write problem in Firefox
|
|
Thread rating:  |
johnsonlau - 27 Feb 2007 13:38 GMT When I use document.write to replace the comment of a frame, I found that the page was always showing as loading in Firefox (2.0.0.2). But it seems that it is OK in IE6. Could someone help me with this problem? Thanks.
My file:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <script> function writeWindow (w2) { var w = w2.open('','_self'); w.document.open();
var a = [ '<html><head><title>No bugs</title>', '<script language="JavaScript" src="js/jsInclude.js"><\/script>', '<script language="JavaScript">function timerOut() {alert("test"); }function t() {var timerObj = new Timer(5000, timerOut);timerObj.start();}<\/script>', '</head><body onload="javascript: t()">', ' <h1>It works</h1>', '<p>Add lots more HTML here...</p>', '</body></html>', ];
var d = w.document; w.document.write(a.join('')); d.close(); }
function load() { writeWindow(top._displayFrame1); }
function test() { window.setTimeout(load, 2000); } </script> </head> <frameset name="frames" id="frames" cols="1024,0" frameborder="0" border="0" onload="test()"> <frameset id = "_displayFrames" cols="800,200" frameborder="0" border="0"> <frame src="about:blank" name="_displayFrame1" id="_displayFrame1" border="no" /> <frame src="about:blank" name="_displayFrame2" id="_displayFrame2" border="no" /> </frameset> <frame src="about:blank" id="ControlFrame" name="ControlFrame" border="no" /> <noframes> </noframes> </frameset> </html>
johnsonlau - 27 Feb 2007 13:53 GMT Oh, it will be fine if I remove all "document.write" in the included JS file "js/jsInclude.js", but failed when any one exists in that file.
Is there any way to solve this problem? Thanks.
shimmyshack - 27 Feb 2007 14:05 GMT > Oh, it will be fine if I remove all "document.write" in the included > JS file "js/jsInclude.js", > but failed when any one exists in that file. > > Is there any way to solve this problem? > Thanks. Sheesh, it is a bit of a cludge, anything you do to hack it up to work is deckchairs on the titanic really. I would accept it's limitations and concentrate on learning more modern methods - document.write has been deprecated for years, you are beginning to experience why - like DOM insertion using standard javascript methods. Is there after all a reason _why_ you need to include the javascript rather than simply copy and paste it into the array, which you later join... I'm not being mean when I say you are making a rod for your own back here. Think about how it will feel in 4 months when you need to tweak this.
-Lost - 27 Feb 2007 14:39 GMT > Sheesh, it is a bit of a cludge, anything you do to hack it up to work > is deckchairs on the titanic really. I would accept it's limitations [quoted text clipped - 7 lines] > here. Think about how it will feel in 4 months when you need to tweak > this. In general, I agree with your overall theory about causing more stress on yourself but you are flat out wrong on the document method "write" being deprecated. It is not.
-Lost
johnsonlau - 28 Feb 2007 13:51 GMT > > Oh, it will be fine if I remove all "document.write" in the included > > JS file "js/jsInclude.js", [quoted text clipped - 14 lines] > here. Think about how it will feel in 4 months when you need to tweak > this. I do know that use of document.write is not recommended today, but I do need this to make the page rewrotten completely in some case. I've tried the DOM using the function shown as below. But when I define the body's onload handler, I can't get it invoked on the document loading process on IE, whereas it did work in FireFox. If this could be solved, I will consider use DOM rather document.write.
=================================================================== xml data send to the client =================================================================== <html> <head> <title>Test Title</title> <script language="JavaScript" src="js/jsInclude.js"></script> <script language="JavaScript"> function timeout() { alert("time out!"); }
function test() { window.setTimeout(timeout, 5000); } </script> </head> <body onload="javascript: test()"> <span name="test.message" id="test.message">Test Message1213</span> </body> </html>
=================================================================== script that used to rewrite the page ===================================================================
function xmlNode2DocNode(doc, docParent, xmlNodes) { var docNode, xmlNode, attr; var styleNode; var i, j; var attrs = new Array();
for (i = 0; i < xmlNodes.length; i++) { xmlNode = xmlNodes[i]; if(xmlNode.nodeType == TEXT_NODE) { if (docParent != null && ((docParent.nodeName.toLowerCase() == "script") || (docParent.nodeName.toLowerCase() == "title"))) { docParent.text += xmlNode.nodeValue; continue; } docNode = doc.createTextNode(xmlNode.nodeValue); } else { var nodeName = xmlNode.nodeName.toLowerCase(); docNode = doc.createElement(nodeName); if (xmlNode.attributes != null) { for (j = 0; j < xmlNode.attributes.length; j++) { var attr = xmlNode.attributes[j]; var attrName = attr.name.toLowerCase(); var attrValue = attr.value; if (attrName == "src" || (attrName == "href" && nodeName != "base")) { attrValue = GetBase(doc) + attrValue; } docNode.setAttribute(attr.name, attrValue); if (attrName.substr(0, 2) == "on") { var func = new Function(attrValue); docNode[attrName] = func; } } } } docParent.appendChild (docNode); xmlNode2DocNode(doc, docNode, xmlNode.childNodes); } }
function GetBase(doc) { var oBaseColl = doc.getElementsByTagName("base"); return ((oBaseColl && oBaseColl.length) ? oBaseColl[0].href : ""); }
function includeJsFile(file) { var doc = document; var docParent = doc.getElementsByTagName("head")[0]; var docNode = doc.createElement("script"); var dir = GetBase (doc); docNode.src = dir + file; docParent.appendChild (docNode); }
function createNewDocument (doc, content) { var newDoc = doc.open("text/html", "replace"); newDoc.close();
var newNode = newDoc.getElementsByTagName("html")[0]; if (typeof newNode != 'undefined') { while (newNode.childNodes.length > 0) { newNode.removeChild(newNode.childNodes[0]); } } xmlNode2DocNode(newDoc, newNode, content.childNodes); }
-Lost - 27 Feb 2007 14:39 GMT > Oh, it will be fine if I remove all "document.write" in the included > JS file "js/jsInclude.js", > but failed when any one exists in that file. > > Is there any way to solve this problem? Yes, there is, use document.close().
-Lost
shimmyshack - 27 Feb 2007 15:08 GMT > > Oh, it will be fine if I remove all "document.write" in the included > > JS file "js/jsInclude.js", [quoted text clipped - 5 lines] > > -Lost Technically Lost is correct of course, document.write can be found in js1.7 for instance. But it's passé, you can choose to use it if you like, but back in 2004 when I (belatedly I felt) started using DOM methods to insert blocks of html into other blocks of html, things got a whole lot easier, although the w3 methods are a little clumsy compared to a simple document.write call, they do tend to be a tad more modular, so for large web apps you just can't beat 'em.
Richard Cornford - 27 Feb 2007 15:48 GMT <snip>
> Technically Lost is correct of course, That would depend a bit what definition of deprecated you wanted to use. People certainly do deprecate the use of - document.write -, but no applicable specification has declared it deprecated.
> document.write can be found in js1.7 for instance. No it can not. JavaScript(tm) abandoned its linkage between the host environment and the language itself with version 1.4. The - document.write - method is specified in the W3C HTML DOM (and not deprecated there).
<snip>
> ... , although the w3 methods are a little clumsy > compared to a simple document.write call, ... <snip>
That does not quite work as a comparison, given that - document.write - is a "w3 method".
Richard.
johnsonlau - 28 Feb 2007 13:38 GMT > > Oh, it will be fine if I remove all "document.write" in the included > > JS file "js/jsInclude.js", [quoted text clipped - 5 lines] > > -Lost When can I invoke document.close? I failed in it immediately after w.document.write(a.join('')), whether I invoke close method on top._displayFrames1.document or the new created document object through the calling of document.open. But if I set a 1 second timer to invoke document.close, the page stopped loading after 1s. It did solved, but it seems that hard to control the timer. Is that any good idea to close the document?
My js/jsInclude.js file. ====================================
function includeJsFile(file) { var script = "<script language=\"JavaScript\" src=\"" + file + "\"></ script>"; document.writeln (script); }
includeJsFile("js/const.js");
|
|
|