IFRAME initalization and stylesheet in DOM-created IFRAME

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vincent van Beveren

    IFRAME initalization and stylesheet in DOM-created IFRAME

    Hi everyone

    I have a JavaScript app that creates an IFRAME through DOM
    (createElement( 'IFRAME')) However, that IFRAME does not have any content
    yet.

    alert(iframe.co ntentWindow.doc ument.documentE lement)

    gives null.

    How can I, through DOM, create the root element? I can't find it.

    My following question: In that IFRAME I need to load a stylesheet.
    However, I can't seem to get the LINK in IFRAME thing working. I tried,
    but IE 6 just seems to ignore the stylesheet speicified in the LINK. I
    have the following code, in which win is the window of the IFRAME
    (iframe.content Window).

    eLink = win.document.cr eateElement("LI NK");
    eLink.setAttrib ute("REL","styl esheet");
    eLink.setAttrib ute("HREF", GUIstyleSheet);
    eLink.setAttrib ute("TYPE","tex t/css");
    win.document.do cumentElement.a ppendChild(eLin k);

    But this:

    win.document.op en();
    win.document.wr iteln("<HTML><H EAD>");
    win.document.wr iteln("<LINK REL=\"styleshee t\"
    HREF=\""+GUIsty leSheet+"\" TYPE=\"text/css\">");
    win.document.wr iteln("</HEAD><BODY></BODY></HTML>");
    win.document.cl ose();

    does work? Does anyone know why? Or what I am doing wrong?

    Thanks!
    Vincent

  • DU

    #2
    Re: IFRAME initalization and stylesheet in DOM-created IFRAME

    Vincent van Beveren wrote:
    [color=blue]
    > Hi everyone
    >
    > I have a JavaScript app that creates an IFRAME through DOM
    > (createElement( 'IFRAME')) However, that IFRAME does not have any content
    > yet.
    >[/color]

    and it won't until you assign the src value.


    [color=blue]
    > alert(iframe.co ntentWindow.doc ument.documentE lement)
    >[/color]

    contentWindow refers to a window object, just as iframe. contentWindow
    is not a DOM 2 attribute anyway.
    [color=blue]
    > gives null.
    >
    > How can I, through DOM, create the root element? I can't find it.
    >
    > My following question: In that IFRAME I need to load a stylesheet.
    > However, I can't seem to get the LINK in IFRAME thing working. I tried,
    > but IE 6 just seems to ignore the stylesheet speicified in the LINK. I
    > have the following code, in which win is the window of the IFRAME
    > (iframe.content Window).
    >
    > eLink = win.document.cr eateElement("LI NK");
    > eLink.setAttrib ute("REL","styl esheet");[/color]

    eLink.rel = "stylesheet ";


    Whenever you can, you should avoid setAttribute as a way to assign an
    attribute; setAttribute does not work well in all browsers.

    [color=blue]
    > eLink.setAttrib ute("HREF", GUIstyleSheet);[/color]

    I assume that GUIstyleSheet is a DOMstring variable.

    eLink.href = GUIstyleSheet;

    [color=blue]
    > eLink.setAttrib ute("TYPE","tex t/css");[/color]

    eLink.type = "text\/css";

    [color=blue]
    > win.document.do cumentElement.a ppendChild(eLin k);
    >[/color]

    win.document.he ad.appendChild( eLink);

    DU
    [color=blue]
    > But this:
    >
    > win.document.op en();
    > win.document.wr iteln("<HTML><H EAD>");
    > win.document.wr iteln("<LINK REL=\"styleshee t\"
    > HREF=\""+GUIsty leSheet+"\" TYPE=\"text/css\">");
    > win.document.wr iteln("</HEAD><BODY></BODY></HTML>");
    > win.document.cl ose();
    >
    > does work? Does anyone know why? Or what I am doing wrong?
    >
    > Thanks!
    > Vincent
    >[/color]

    Comment

    • Vincent van Beveren

      #3
      Re: IFRAME initalization and stylesheet in DOM-created IFRAME

      >[color=blue][color=green]
      >> Hi everyone
      >>
      >> I have a JavaScript app that creates an IFRAME through DOM
      >> (createElement( 'IFRAME')) However, that IFRAME does not have any content
      >> yet.
      >>[/color]
      >
      > and it won't until you assign the src value.
      >[/color]

      But thats weird. It must be possible to create an empty document and
      fill it dynamically, right? Else I'll have to do

      iframe.src='abo ut:blank';

      and thats ugly (I think).

      Vincent

      ps Thanks for the CSS info, that helped

      Comment

      • Randy Webb

        #4
        Re: IFRAME initalization and stylesheet in DOM-created IFRAME

        Vincent van Beveren wrote:
        [color=blue][color=green]
        >>[color=darkred]
        >>> Hi everyone
        >>>
        >>> I have a JavaScript app that creates an IFRAME through DOM
        >>> (createElement( 'IFRAME')) However, that IFRAME does not have any content
        >>> yet.
        >>>[/color]
        >>
        >> and it won't until you assign the src value.
        >>[/color]
        >
        > But thats weird. It must be possible to create an empty document and
        > fill it dynamically, right? Else I'll have to do
        >
        > iframe.src='abo ut:blank';[/color]

        iframe.src = 'blank.html';

        where blank.html is a valid blank page.

        Then, you modify the DOM of blank.html

        If you run it from a web server, and try to modify the DOM of
        about:blank, you will run into security issues.

        --
        Randy
        Chance Favors The Prepared Mind
        comp.lang.javas cript FAQ - http://jibbering.com/faq/

        Comment

        • Vincent van Beveren

          #5
          Re: IFRAME initalization and stylesheet in DOM-created IFRAME

          >>>> I have a JavaScript app that creates an IFRAME through DOM[color=blue][color=green][color=darkred]
          >>>> (createElement( 'IFRAME')) However, that IFRAME does not have any
          >>>> content
          >>>> yet.
          >>>>
          >>>
          >>> and it won't until you assign the src value.
          >>>[/color]
          >>
          >> But thats weird. It must be possible to create an empty document and
          >> fill it dynamically, right? Else I'll have to do
          >>
          >> iframe.src='abo ut:blank';[/color]
          >
          >
          > iframe.src = 'blank.html';
          >[/color]

          But thats still not very neat. I mean, its not logical to first have to
          load an external html file inorder to dynamically create one? Shouldn't
          something like

          iframe.contentD ocument = new Document(those nice W3C things);

          or

          iframe.createCo ntentDocument(t hose nice W3C things);

          work/be implemented?

          Vincent

          Comment

          • ExGuardianReader

            #6
            Re: IFRAME initalization and stylesheet in DOM-created IFRAME

            Vincent van Beveren wrote:
            [color=blue][color=green][color=darkred]
            >>>>> I have a JavaScript app that creates an IFRAME through DOM
            >>>>> (createElement( 'IFRAME')) However, that IFRAME does not have any
            >>>>> content
            >>>>> yet.
            >>>>>
            >>>>
            >>>> and it won't until you assign the src value.
            >>>>
            >>>
            >>> But thats weird. It must be possible to create an empty document and
            >>> fill it dynamically, right? Else I'll have to do
            >>>
            >>> iframe.src='abo ut:blank';[/color]
            >>
            >>
            >>
            >> iframe.src = 'blank.html';
            >>[/color]
            >
            > But thats still not very neat. I mean, its not logical to first have to
            > load an external html file inorder to dynamically create one? Shouldn't
            > something like
            >
            > iframe.contentD ocument = new Document(those nice W3C things);
            >
            > or
            >
            > iframe.createCo ntentDocument(t hose nice W3C things);
            >
            > work/be implemented?
            >
            > Vincent
            >[/color]
            IE thinks about:blank is a security risk when loading the main page
            using https, and causes mixed mode warnings.

            I've used

            iframe.src = "javascript:par ent.blank()"

            to get around this. Where in the main window, you have a function

            function blank()
            {
            return "<html></html>";
            }

            Again, it's ugly, but it works *now.*

            Nige

            Comment

            • Jim Ley

              #7
              Re: IFRAME initalization and stylesheet in DOM-created IFRAME

              On Sat, 3 Jul 2004 10:38:35 +0000 (UTC), ExGuardianReade r
              <noway@noway.co m> wrote:
              [color=blue]
              >iframe.src = "javascript:par ent.blank()"[/color]

              Recently I've had security problems with this too on cross window
              iframe references in IE5.5sp2 fully patched. load a blank html doc
              from your domain.

              Jim.
              --
              comp.lang.javas cript FAQ - http://jibbering.com/faq/

              Comment

              • DU

                #8
                Re: IFRAME initalization and stylesheet in DOM-created IFRAME

                Vincent van Beveren wrote:[color=blue][color=green][color=darkred]
                >>>>> I have a JavaScript app that creates an IFRAME through DOM
                >>>>> (createElement( 'IFRAME')) However, that IFRAME does not have any
                >>>>> content
                >>>>> yet.
                >>>>>
                >>>>
                >>>> and it won't until you assign the src value.
                >>>>
                >>>
                >>> But thats weird. It must be possible to create an empty document and
                >>> fill it dynamically, right? Else I'll have to do
                >>>
                >>> iframe.src='abo ut:blank';[/color]
                >>
                >>
                >>
                >> iframe.src = 'blank.html';
                >>[/color]
                >
                > But thats still not very neat. I mean, its not logical to first have to
                > load an external html file inorder to dynamically create one? Shouldn't
                > something like
                >
                > iframe.contentD ocument = new Document(those nice W3C things);
                >
                > or
                >
                > iframe.createCo ntentDocument(t hose nice W3C things);
                >
                > work/be implemented?
                >
                > Vincent
                >[/color]

                You have a good question here, I'd say. Apparently, you should be able
                to do that but I have not found a single example of working code
                anywhere and I tried my own and never could achieve anything despite
                following thoroughly all documentations.

                createDocument


                E.g.:

                var PopupDoctype = document.implem entation.create DocumentType("" ,
                "!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'",
                "http://www.w3.org/TR/html4/strict.dtd");
                var PopupDOMDocumen t = document.implem entation.create Document("",
                "html", PopupDoctype);
                PopupDOMDocumen t.title = "title here";
                var objPopupBody = PopupDOMDocumen t.createElement ("body");
                etc..

                If you can't use that (I couldn't achieve results in any browsers), then
                just use an "about:blan k" document and populate it dynamically. That
                will work in many browsers. I have working examples on this.

                There's also
                createDocumentF ragment

                "DocumentFragme nt is a "lightweigh t" or "minimal" Document object."


                DU

                Comment

                Working...