On 2ÔÂ27ÈÕ, ÏÂÎç11ʱ05·Ö, "shimmyshack" <matt.fa...@gmail.comwrote:
On 27 Feb, 13:53, "johnsonlau" <laozhongch...@gmail.comwrote:
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.
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);
}