Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dynamic IFRAME elements

0 views
Skip to first unread message

Joost Devos

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to
As one can add/remove dynamically option elements to a listbox, is it also
possible to dynamically add/remove iframe elements in a similar way ?

oListbox[oListbox.length] = new Option("MyOption", "MyValue")

so...
var oFrame = new IFrame() doesn't work...Error: IFrame is undefined
I also tried IFRAME(), iframe(), Iframe()
Where to find info on the objects that can be created through JScript ?

TIA
Joost Devos

Peter Shore

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to

"Joost Devos" <Joost...@dbinfo.be> wrote in message news:##$m4tVLAHA.243@cppssbbsa04...

> As one can add/remove dynamically option elements to a listbox, is it also
> possible to dynamically add/remove iframe elements in a similar way ?

Sure, a new <iframe> document element can be dynamically created just like any other HTML element . . .

<html><head>
<style type="text/css">
Iframe
{ width:85%; height:expression(document.body.clientHeight*.7);
clear:left; margin-Top:2em; border:16px groove orange}
</style>

<script language="jscript">
function addIFrame(){var x;
with(document) x=body.appendChild(createElement('IFRAME'));
with(x){ name='myNewFrame'; align='center'; src='http://microsoft.com'}}
</script>
</head>

<body>
<button onclick="addIFrame()">Add An Iframe</button>
</body></html>

------------------------------------------

Not sure if that works in IE4, but basically, every element created in IE5+ is created the same way.

Assuming that the iframe's SRC is actually a document on your server, from the parent document, you can then do anything in the
iframe that you can with any other frame.

Where you would use "parent.frameName" to address a standard frame, use "document.frames[0]" to address the iframe.

- Peter

Joost Devos

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to
Thanks a lot Peter,
exactly what I wanted to achieve
Joost

"Peter Shore" <pete...@home.com> wrote in message
news:O7ODH$WLAHA.195@cppssbbsa05...

Peter Shore

unread,
Oct 4, 2000, 1:37:59 AM10/4/00
to

"Joost Devos" <Joost...@dbinfo.be> wrote in message news:uxYJIRYLAHA.208@cppssbbsa03...

> Thanks a lot Peter,
> exactly what I wanted to achieve
> Joost

Glad to be of help, but I forgot to address the second part of your question . . .

> is it also possible to dynamically add/remove iframe elements

Element removal's even simpler . . .

document.all.tags('iframe')[0].removeNode();

That's all there is to it.

- Peter


Peter Shore

unread,
Oct 4, 2000, 3:00:00 AM10/4/00
to

"Peter Shore" <pete...@home.com> wrote in message news:O7ODH$WLAHA.195@cppssbbsa05...
>
> "Joost Devos" <Joost...@dbinfo.be> wrote in message news:##$m4tVLAHA.243@cppssbbsa04...
> > As one can add/remove dynamically option elements to a listbox, is it also

> > possible to dynamically add/remove iframe elements in a similar way ?

> <script language="jscript">
> function addIFrame(){var x;
> with(document) x=body.appendChild(createElement('IFRAME'));
> with(x){ name='myNewFrame'; align='center'; src='http://microsoft.com'}}
> </script>

After posting the above, I subsequently realized that, at least in IE5.5, although no error is produced & the iframe is created
normally, the stated name attribute gets ignored, and no name actually gets assigned to the newly created iframe .

If you don't have any hyperlinks with a "target" pointing to the new iframe, the ommision's inconsequential. In script, you can
always change the iframe's location with

document.frames[0].location="someFile.htm"


However, if you have (or will be creating) HTML links like <a href="someFile.htm" target="myNewFrame">Click Here</a>, instead of the
document opening in the iframe, as expected, it would open in a new browser window - since the iframe has no name.


If you do need a name attribute included in the created iframe, outerHTML can be used to force IE to accept inclusion of a name
attribute. The following modified version of the script above would do the trick . . .


function addIFrame(){ var x,y;
y=' name="myNewFrame" align=center src="myURL.htm">';
with(document) x=body.appendChild(createElement('IFRAME'));
y=x.outerHTML.replace(/>/,y);
x.outerHTML=y}


------------------

Hope that helps somebody out.

- Peter

Joost Devos

unread,
Oct 4, 2000, 3:00:00 AM10/4/00
to

> Hope that helps somebody out.
It surely will Peter...

an additional suggestion.
==> pass a parameter to the function eg. addIFrame(strFramename)

so your code becomes more dynamic...
> function addIFrame(strFramename){ var x,y;
> y=' name="'+strFramename+'" align=center src="myURL.htm">';


> with(document) x=body.appendChild(createElement('IFRAME'));
> y=x.outerHTML.replace(/>/,y);
> x.outerHTML=y}

for me personally, I only need a collection of nameless <iframe> elements

New question:
Is it possible to store the whole iframe object (including script) in a
variable, remove the <iframe> element, create a new <iframe> element and put
the previous stored object (including script) into that <iframe> element
????

TIA,
Joost Devos

"Peter Shore" <pete...@home.com> wrote in message

news:uBjAt5i...@cppssbbsa02.microsoft.com...

Peter Shore

unread,
Oct 5, 2000, 3:00:00 AM10/5/00
to

"Joost Devos" <Joost...@dbinfo.be> wrote in message news:OwALi1k...@cppssbbsa02.microsoft.com...

> New question:

> Is it possible to store the whole iframe object (including script) in a
> variable, remove the <iframe> element, create a new <iframe> element and put
> the previous stored object (including script) into that <iframe> element
> ????

Yes.

var x=document.frames[0].document.documentElement.outerHTML;

But you'll find that to be, in practice, a bag of worms.

- Peter

Joost Devos

unread,
Oct 6, 2000, 3:00:00 AM10/6/00
to

Tx for the hint Peter,

I'll put my gas-mask on when I'm digging into the bag of worms...

Joost

"Peter Shore" <pete...@home.com> wrote in message

news:#NEw0xsLAHA.261@cppssbbsa05...

0 new messages