472,121 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

How to write a new iframe from inline JS?

ok
<img src="http://www.6park.com/enter2/av.gif" onload="document.write('an
iframe obj');">

First of all, it destroy the HTML page.

Secondly even if it does not destroy it, the iframe object will be within an
img tag.

How do I write a new iframe obj outside img tag?
Jul 23 '05 #1
6 12415


ok wrote:
<img src="http://www.6park.com/enter2/av.gif" onload="document.write('an
iframe obj');">

First of all, it destroy the HTML page.

Secondly even if it does not destroy it, the iframe object will be within an
img tag.

How do I write a new iframe obj outside img tag?


You can use the DOM to create an iframe dynamically with IE5+, Netscape
6+, Opera 7 and other DOM compliant browsers:

function createIframe (iframeName, iframeSrc) {
var iframe;
if (document.createElement && (iframe =
document.createElement('iframe'))) {
iframe.name = iframeName;
iframe.src = iframeSrc;
document.body.appendChild(iframe);
}
}

<img onload="createIframe('iframeName', 'whatever.html');"
src="whatever.gif" alt="what ever">

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
ok
> <img src="http://www.6park.com/enter2/av.gif" onload="document.write('an
iframe obj');">

First of all, it destroy the HTML page.

Secondly even if it does not destroy it, the iframe object will be within an img tag.

How do I write a new iframe obj outside img tag?


By the way, a plain IFRAME tag is not allowed anywhere in the html
Jul 23 '05 #3
"ok" <a@b.c> wrote in message news:<41******@news01.argolink.net>...
<img src="http://www.6park.com/enter2/av.gif" onload="document.write('an
iframe obj');">
This is a strange directive. What you are saying is that when the
image gets loaded from the server, you want run the function
document.write. Assuming for a moment that document.write might do
something valid, it would insert into the document the phrase 'an
iframe obj'. Document.write takes as input normal html.
You would expect something like:
document.write("<iframe src='myiframe.html'><\/iframe>");
However, this wouldn't work in your situation.

It would appear that you are trying to set a message in the page when
the loading of the image is complete.

First of all, it destroy the HTML page.
How true. Document.write is designed to be used as the html is being
constructed. Once a web browser see the </html> tag, you can no
longer use the document.write function.

How do I write a new iframe obj outside img tag?


I assumed you were using the image as a button and wrote the follow
code. When I wrote the code, I didn't realize that you where using
the onload event handler. I assumed onclick. To change to the onload
do something like:
<img src="http://www.6park.com/enter2/av.gif"
onload="insertIframe();">

Notice that I have a div where the iframe will be inserted.

I tested this with macos 10.2.6 with IE 5.2 and netscape 7.3.

Robert
insert.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>insert an iframe</title>

<script type="text/javascript">
function insertIframe ()
{
var node;
if (document.getElementById)
{
node = document.getElementById("iframediv");
if (node)
{
var myElement = document.createElement("DIV");
node.appendChild(myElement);

myElement.innerHTML =
"<iframe src='myiframe.html' " +
"width='400' height='160'></iframe> " ;
}
else
{
alert("The function insertIframe could not find " +
"the tag iframediv.");
return;
}
}
else
{
alert(" document.getElementByID failed. " );
return;
}

}
</script>
</head>
<body>
<p>Insert an iframe.
<br>
Don't forget to see what happens when you
press the button more than once.</p>
<form>
<input type="button"
name="activate"
value="insert the iframe"
onclick="insertIframe();">
<br>
</form>
<div id='iframediv'>
</div>
</body>
</html>

myiframe.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>this is myiframe</title>
</head>
<body>
<p>This iframe was inserted by innerHTML.
<br>
This is amazing!</p>
</body>
</html>
Jul 23 '05 #4
Robert wrote:
"ok" <a@b.c> wrote in message news:<41******@news01.argolink.net>...


<--snip-->
First of all, it destroy the HTML page.

How true. Document.write is designed to be used as the html is being
constructed. Once a web browser see the </html> tag, you can no
longer use the document.write function.


Yes you can.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Blank Test Page</title>
<style type="text/css">
</style>
<script type="text/javascript">
</script>
</head>
<body>
Body Text
</body>
</html>
<script type="text/javascript">
document.write('New Body Text')
</script>
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #5
Randy Webb <Hi************@aol.com> wrote in message news:<Xt********************@comcast.com>...
Robert wrote:
"ok" <a@b.c> wrote in message news:<41******@news01.argolink.net>...


<--snip-->
First of all, it destroy the HTML page.

How true. Document.write is designed to be used as the html is being
constructed. Once a web browser see the </html> tag, you can no
longer use the document.write function.


Yes you can.


So I see. Any utility in it? Why would I want to do this? Is it
really valid since there should be no tags nor data after the </html>
that I am aware?

This should suffice:
Once a web browser see the </html> tag, you should no
longer use the document.write function.
You can always use the document.write as the OP did, but it will not
likely yeild the results expected ;-).

Robert
Jul 23 '05 #6
Robert wrote:
Randy Webb <Hi************@aol.com> wrote in message news:<Xt********************@comcast.com>...
Robert wrote:
"ok" <a@b.c> wrote in message news:<41******@news01.argolink.net>...
<--snip-->
First of all, it destroy the HTML page.
How true. Document.write is designed to be used as the html is being
constructed. Once a web browser see the </html> tag, you can no
longer use the document.write function.


Yes you can.

So I see. Any utility in it? Why would I want to do this? Is it
really valid since there should be no tags nor data after the </html>
that I am aware?


No real utility in it, just a reality (most of the time), with ad
inserting servers that either prepend the ad code, or append it to the
document. In that event, you can end up with some really wierd things
before the DTD and after the initial </html> and no its nowhere close to
valid code.
This should suffice:
Once a web browser see the </html> tag, you should no
longer use the document.write function.
Yes, that suffices.
You can always use the document.write as the OP did, but it will not
likely yeild the results expected ;-).


How true :) Depending on who is predicting the expected results :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by John Chen | last post: by
2 posts views Thread by H.A.P | last post: by
4 posts views Thread by Keon | last post: by
2 posts views Thread by zhanye815 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.