reload page, document.write; external js problems

R

robert.waters

Hello,

I am dynamically editing the contents of a page and using
document.write() to reload the new, edited content. However, on the
new page, any calls to functions located in external .js files (fyi:
same domain) fail with 'object expected' (IE6). If I subsequently
reload the page, everything works. Also, if I inline this code,
everything works.
Here is an example: (called from body onload event)
(This just rewrites the page with the function to rewrite it removed;
what I'm doing is more complicated)
start
<script src="path-to-script.js">
<script>
function test() {
// I'm actually using xmlhttp GET (irrelevant)
var h = document.getElementByTagName('html').innerHTML;
h = h.replace(/start(.*?)end/m,'');
document.open();
document.write(h);
document.close();
}
</script>
end
<script>call-function-from-file(parameters);</script> <---- error
object expected

I *really* don't want to inline the code, as it is complicated and
voluminous.
I've played with other methods of executing the document.write(), using
setTimeout(), different parameters to document.open(), different
attributes for the script tag, etc... - everything is the same.
There is no evidence that the browser is even attempting to load these
files; if I remove them from the server completely, the errors are
exactly the same.

Any help is appreciated.

Thanks in advance,
Robert Waters
 
M

Martin Honnen

I am dynamically editing the contents of a page and using
document.write() to reload the new, edited content.
(This just rewrites the page with the function to rewrite it removed;
what I'm doing is more complicated)
start
<script src="path-to-script.js">

Where is the closing said:
<script>
function test() {
// I'm actually using xmlhttp GET (irrelevant)
var h = document.getElementByTagName('html').innerHTML;

Perhaps you want
var h = document.documentElement.innerHTML
? Or at least
var h = document.getElementsByTagName('html')[0].innerHTML;
?
h = h.replace(/start(.*?)end/m,'');
document.open();
document.write(h);
document.close();

So the new markup written has no <html> root element at all as you use
innerHTML of the root element?

And browsers for many years by now have various object models allowing
you to replace or manipulate parts of the document, why on earth are you
trying to rewrite the complete HTML markup with some string replacement
applied?
Ever heard of the IE DOM (e.g. setting and getting innerHTML and
outerHTML on elements exposed in the DOM tree, insertAdjacentHTML) and
the W3C DOM (replaceChild, removeChild)?
 
R

robert.waters

Thank you for your help, and your tips, and I am sorry to have taken up
so much of your time with mistakes related to the example that I made
for brevity's sake.

Below is a working example that will demonstrate that, when
document.write() is called using the current document's innerHTML, any
scripts referenced in an external file do not load a second time - this
causes script errors ('object expected') when the second page is
loading (using IE6).
Note that I have omitted document.open, since document.write implies it
(and it's inclusion causes no difference in IE's behaviour that I can
see):
------------test.html--------------------
<html>
<head>
<script src="script.js"></script>
<script type="text/javascript">
function rewritePage(){
alert('rewriting the page');
document.write(document.documentElement.innerHTML);
}
</script>
</head>
<body onload="rewritePage();">
generate a script error with document.write
<script type="text/javascript">
isScriptWorking(); // from script.js
</script>
</body>
</html>
---------script.js-------------
function isScriptWorking() {
alert('script is working');
}

Martin - if you are truly interested in why I must implement this, I
would love to explain it; I omitted it to not burden the group with my
implementation details unnecessarily. Thank you for your help so far,
it is appreciated. I understand that your time is valuable, and that
many of the problems posted on usenet can be resolved by the poster
with very little work, and it is frustrating. I realize that I could
have explained myself better, and should have used a better example
initially. -Robert
P.S. I really do believe that this is a browser issue, as I cannot find
reference to it in any of the msdn documentation, nor the DOM specs as
written by W3C, nor google, except for unanswered/worked-around posts
on this group (3 in the past several years, as archived by google).
Firefox 1.0.7 works as expected (perfectly ;) in this instance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top