a puzzling document.write behavior

Discussion in 'Javascript' started by VK, Mar 29, 2008.

  1. VK

    VK Guest

    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.
    VK, Mar 29, 2008
    #1
    1. Advertising

  2. VK

    Tom de Neef Guest

    "VK" <> 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
    Tom de Neef, Mar 29, 2008
    #2
    1. Advertising

  3. VK

    VK Guest

    On Mar 29, 10:27 pm, "Tom de Neef" <> wrote:
    > "VK" <> 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.
    VK, Mar 29, 2008
    #3
  4. VK

    Lee Guest

    VK said:
    >
    >On Mar 29, 10:27 pm, "Tom de Neef" <> wrote:
    >> "VK" <> 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().


    --
    Lee, Mar 29, 2008
    #4
  5. VK

    VK Guest

    On Mar 30, 3:54 am, Lee <> wrote:
    > VK said:
    >
    >
    >
    >
    >
    > >On Mar 29, 10:27 pm, "Tom de Neef" <> wrote:
    > >> "VK" <> 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/Option3_03_corrected.html
    VK, Mar 30, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    1
    Views:
    966
    Chris Smith
    Apr 15, 2004
  2. Replies:
    3
    Views:
    281
  3. Replies:
    0
    Views:
    272
  4. Jav
    Replies:
    3
    Views:
    148
  5. Replies:
    5
    Views:
    111
Loading...

Share This Page