a puzzling document.write behavior

V

VK

We need to load a document into IFRAME with editing mode being turned
on by default. SRC attribute cannot be hardcoded and it will set on
page load. In the sample it is assumed that some of
preliminary loaded scripts has function getUrlForIframe that takes
string argument with the name of iframe and returns src value for that
iframe.

See: http://www.russiancourses.org/tmp/Option3.html

The sample code seemingly works for a single iframe, but after that no
other script block is being executed. The function is being executed
without runtime errors as window.alert shows, but any other script is
simply disregarded. The most weird behavior - or maybe I am missing
something
obvious. Any feedback is highly appreciated.
 
T

Tom de Neef

VK said:
We need to load a document into IFRAME with editing mode being turned
on by default. SRC attribute cannot be hardcoded and it will set on
page load. In the sample it is assumed that some of
preliminary loaded scripts has function getUrlForIframe that takes
string argument with the name of iframe and returns src value for that
iframe.

See: http://www.russiancourses.org/tmp/Option3.html

The sample code seemingly works for a single iframe, but after that no
other script block is being executed. The function is being executed
without runtime errors as window.alert shows, but any other script is
simply disregarded. The most weird behavior - or maybe I am missing
something
obvious. Any feedback is highly appreciated.

I think that document.write() will result in the creation of a new namespace
(I've probably got the terminology wrong) meaning that all your global
variables have lost their values. Could that be the cause of the behaviour
you observe?
Tom
 
V

VK

I think that document.write() will result in the creation of a new namespace
(I've probably got the terminology wrong)

"creating new Global object" or "creating new global execution
context"
meaning that all your global
variables have lost their values. Could that be the cause of the behaviour
you observe?

It could be if we were writing to the host window: but we are writing
to iframe, so the host window remains intact. I have modified the
sample page
http://www.russiancourses.org/tmp/Option3_02.html

<!-- BLOCK 1 -->
<script type="text/javascript">
(function(){
var name = 'doc02';
document.write(''.concat(
'<iframe name="',name,'" ',
'src="',getUrlForIframe(name),'" ',
'onload="',
'(function(){',
'var n = \'',name,'\';',
'var win = self.frames[n];',
'var doc = win.document;',
'if ((doc) && (\'designMode\' in doc)) {',
' doc.designMode = \'on\';',
'}','})();"'));
window.alert('Fine up to this point');
})();
window.alert('getUrlForIframe is ' + typeof getUrlForIframe);
window.setTimeout('window.alert(true)',1000);
</script>

It shows that the script engine keeps functioning, even 1sec after
leaving the execution context. It just _silently_ disregard any
further script blocks as if they were stripped out. Moreover this
behavior is consistent for all browsers I am testing with: IE,
Firefox, Opera, Safari. So it is not a browser-specific bug, it more
looks like some dark secret of Javascript/DOM I was not aware about
ever before.
 
L

Lee

VK said:
I think that document.write() will result in the creation of a new namespace
(I've probably got the terminology wrong)

"creating new Global object" or "creating new global execution
context"
meaning that all your global
variables have lost their values. Could that be the cause of the behaviour
you observe?

It could be if we were writing to the host window: but we are writing
to iframe, so the host window remains intact. I have modified the
sample page
http://www.russiancourses.org/tmp/Option3_02.html

<!-- BLOCK 1 -->
<script type="text/javascript">
(function(){
var name = 'doc02';
document.write(''.concat(
'<iframe name="',name,'" ',
'src="',getUrlForIframe(name),'" ',
'onload="',
'(function(){',
'var n = \'',name,'\';',
'var win = self.frames[n];',
'var doc = win.document;',
'if ((doc) && (\'designMode\' in doc)) {',
' doc.designMode = \'on\';',
'}','})();"'));
window.alert('Fine up to this point');
})();
window.alert('getUrlForIframe is ' + typeof getUrlForIframe);
window.setTimeout('window.alert(true)',1000);
</script>

It shows that the script engine keeps functioning, even 1sec after
leaving the execution context. It just _silently_ disregard any
further script blocks as if they were stripped out. Moreover this
behavior is consistent for all browsers I am testing with: IE,
Firefox, Opera, Safari. So it is not a browser-specific bug, it more
looks like some dark secret of Javascript/DOM I was not aware about
ever before.

You're not writing into an iframe, you're writing an iframe
into the host window. The first internal step performed by
a call to document.write() is document.clear().


--
 
V

VK

VK said:




"creating new Global object" or "creating new global execution
context"
It could be if we were writing to the host window: but we are writing
to iframe, so the host window remains intact. I have modified the
sample page
http://www.russiancourses.org/tmp/Option3_02.html
<!-- BLOCK 1 -->
<script type="text/javascript">
(function(){
var name = 'doc02';
document.write(''.concat(
'<iframe name="',name,'" ',
'src="',getUrlForIframe(name),'" ',
'onload="',
'(function(){',
'var n = \'',name,'\';',
'var win = self.frames[n];',
'var doc = win.document;',
'if ((doc) && (\'designMode\' in doc)) {',
' doc.designMode = \'on\';',
'}','})();"'));
window.alert('Fine up to this point');
})();
window.alert('getUrlForIframe is ' + typeof getUrlForIframe);
window.setTimeout('window.alert(true)',1000);
</script>
It shows that the script engine keeps functioning, even 1sec after
leaving the execution context. It just _silently_ disregard any
further script blocks as if they were stripped out. Moreover this
behavior is consistent for all browsers I am testing with: IE,
Firefox, Opera, Safari. So it is not a browser-specific bug, it more
looks like some dark secret of Javascript/DOM I was not aware about
ever before.

You're not writing into an iframe, you're writing an iframe
into the host window. The first internal step performed by
a call to document.write() is document.clear().

Only if used after document LOAD event which is not the case here.

Lucky I have found the error right at the moment when I was starting
to loose the rest of my mind. How stupid I am: who will close IFRAME?!
I have forgotten to document.write the closing </iframe> tag so the
first IFRAME expanded to the rest of the page. Shame on me... Here the
corrected working version:

http://www.russiancourses.org/tmp/Option3_03_corrected.html
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top