By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,086 Members | 1,442 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,086 IT Pros & Developers. It's quick & easy.

IFRAME initalization and stylesheet in DOM-created IFRAME

P: n/a
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.contentWindow.document.documentElemen t)

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

eLink = win.document.createElement("LINK");
eLink.setAttribute("REL","stylesheet");
eLink.setAttribute("HREF", GUIstyleSheet);
eLink.setAttribute("TYPE","text/css");
win.document.documentElement.appendChild(eLink);

But this:

win.document.open();
win.document.writeln("<HTML><HEAD>");
win.document.writeln("<LINK REL=\"stylesheet\"
HREF=\""+GUIstyleSheet+"\" TYPE=\"text/css\">");
win.document.writeln("</HEAD><BODY></BODY></HTML>");
win.document.close();

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

Thanks!
Vincent

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
DU
Vincent van Beveren wrote:
Hi everyone

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.

http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-43933957
alert(iframe.contentWindow.document.documentElemen t)

contentWindow refers to a window object, just as iframe. contentWindow
is not a DOM 2 attribute anyway.
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.contentWindow).

eLink = win.document.createElement("LINK");
eLink.setAttribute("REL","stylesheet");
eLink.rel = "stylesheet";
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-41369587

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

eLink.setAttribute("HREF", GUIstyleSheet);
I assume that GUIstyleSheet is a DOMstring variable.

eLink.href = GUIstyleSheet;
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-33532588
eLink.setAttribute("TYPE","text/css");
eLink.type = "text\/css";
http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-32498296
win.document.documentElement.appendChild(eLink);

win.document.head.appendChild(eLink);

DU
But this:

win.document.open();
win.document.writeln("<HTML><HEAD>");
win.document.writeln("<LINK REL=\"stylesheet\"
HREF=\""+GUIstyleSheet+"\" TYPE=\"text/css\">");
win.document.writeln("</HEAD><BODY></BODY></HTML>");
win.document.close();

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

Thanks!
Vincent

Jul 23 '05 #2

P: n/a
>
Hi everyone

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='about:blank';

and thats ugly (I think).

Vincent

ps Thanks for the CSS info, that helped

Jul 23 '05 #3

P: n/a
Vincent van Beveren wrote:
Hi everyone

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='about:blank';


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.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #4

P: n/a
>>>> 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='about:blank';

iframe.src = 'blank.html';


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.contentDocument = new Document(those nice W3C things);

or

iframe.createContentDocument(those nice W3C things);

work/be implemented?

Vincent

Jul 23 '05 #5

P: n/a
Vincent van Beveren wrote:
> 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='about:blank';


iframe.src = 'blank.html';


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.contentDocument = new Document(those nice W3C things);

or

iframe.createContentDocument(those nice W3C things);

work/be implemented?

Vincent

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:parent.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

Jul 23 '05 #6

P: n/a
On Sat, 3 Jul 2004 10:38:35 +0000 (UTC), ExGuardianReader
<no***@noway.com> wrote:
iframe.src = "javascript:parent.blank()"


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.javascript FAQ - http://jibbering.com/faq/

Jul 23 '05 #7

P: n/a
DU
Vincent van Beveren wrote:
> 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='about:blank';


iframe.src = 'blank.html';


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.contentDocument = new Document(those nice W3C things);

or

iframe.createContentDocument(those nice W3C things);

work/be implemented?

Vincent


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
http://www.w3.org/TR/2000/REC-DOM-Le...createDocument

E.g.:

var PopupDoctype = document.implementation.createDocumentType("",
"!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN'",
"http://www.w3.org/TR/html4/strict.dtd");
var PopupDOMDocument = document.implementation.createDocument("",
"html", PopupDoctype);
PopupDOMDocument.title = "title here";
var objPopupBody = PopupDOMDocument.createElement("body");
etc..

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

There's also
createDocumentFragment
http://www.w3.org/TR/2000/REC-DOM-Le...ml#ID-35CB04B5
"DocumentFragment is a "lightweight" or "minimal" Document object."
http://www.w3.org/TR/2000/REC-DOM-Le...ml#ID-B63ED1A3

DU
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.