Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > a puzzling document.write behavior

Reply
Thread Tools

a puzzling document.write behavior

 
 
VK
Guest
Posts: n/a
 
      03-29-2008
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.
 
Reply With Quote
 
 
 
 
Tom de Neef
Guest
Posts: n/a
 
      03-29-2008
"VK" <(E-Mail Removed)> wrote
> 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


 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      03-29-2008
On Mar 29, 10:27 pm, "Tom de Neef" <(E-Mail Removed)> wrote:
> "VK" <(E-Mail Removed)> wrote
>
> > 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)


"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.


 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      03-29-2008
VK said:
>
>On Mar 29, 10:27 pm, "Tom de Neef" <(E-Mail Removed)> wrote:
>> "VK" <(E-Mail Removed)> wrote
>>
>> > 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)

>
>"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().


--

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      03-30-2008
On Mar 30, 3:54 am, Lee <(E-Mail Removed)> wrote:
> VK said:
>
>
>
>
>
> >On Mar 29, 10:27 pm, "Tom de Neef" <(E-Mail Removed)> wrote:
> >> "VK" <(E-Mail Removed)> wrote

>
> >> > 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)

>
> >"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().


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/Op...corrected.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Puzzling javadoc behavior rodrigobraz.removethis@gmail.com Java 0 10-18-2007 05:41 AM
More puzzling behavior while subclassing datetime insyte@gmail.com Python 3 11-16-2006 10:52 AM
Puzzling Virtual Directory Behavior Jav ASP General 3 06-11-2006 09:06 PM
puzzling behavior in simple variable comparison aclarke@austin.rr.com Perl Misc 5 09-27-2005 02:33 AM
Puzzling Browser Refresh and session attribute behavior GIMME Java 1 04-14-2004 11:27 PM



Advertisments