Working with IFRAME in JavaScript - How to write content dynamically into IFRAME using JavaScript: another way
(Page 6 of 6 )
The two previous sections demonstrated how to write content dynamically to an IFRAME. But the example I showed you earlier may not be suited for real-world requirements. Now, I would like to present you with another, better example, which could be really helpful! Let us have a look at the following code.
<html>
<head>
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
<script id="clientEventHandlersJS" language="javascript">
<!--
function Show()
{
// get our iframe
var testFrame = document.getElementById("myFrame");
var val;
val = "<TABLE border=1>";
val += "<TR><TD>SlNo</TD><TD>Name</TD></TR>";
val += "<TR><TD>1</TD><TD>Jag</TD></TR>";
val += "<TR><TD>2</TD><TD>Chat</TD></TR>";
val += "<TR><TD>3</TD><TD>Win</TD></TR>";
val += "<TR><TD>4</TD><TD>Dhan</TD></TR>";
val += "</TABLE>";
// now write out the new contents
var doc = testFrame.contentDocument;
if (doc == undefined || doc == null)
doc = testFrame.contentWindow.document;
doc.open();
doc.write(val);
doc.close();
document.all.myFrame.style.visibility="visible";
}
function Button1_onclick() {
Show();
}
//-->
</script>
</head>
<body>
<iframe id="myFrame" frameborder="0" vspace="0" hspace="0" marginwidth="0"
marginheight="0" width="100" scrolling=yes height="100"
style="BORDER-RIGHT: black 1px solid; BORDER-TOP: black 1px solid; Z-INDEX: 999; LEFT: 20px; BORDER-LEFT: black 1px solid; BORDER-BOTTOM: black 1px solid; POSITION: absolute; TOP: 100px; visibility:hidden;"></iframe>
<form id="form1">
<input type="button" value="Show" id="Button1" name="Button1" onclick="return Button1_onclick()">
</form>
</body>
</html>
Within the above code, the only important issue is the following:
var val;
val = "<TABLE border=1>";
val += "<TR><TD>SlNo</TD><TD>Name</TD></TR>";
val += "<TR><TD>1</TD><TD>Jag</TD></TR>";
val += "<TR><TD>2</TD><TD>Chat</TD></TR>";
val += "<TR><TD>3</TD><TD>Win</TD></TR>";
val += "<TR><TD>4</TD><TD>Dhan</TD></TR>";
val += "</TABLE>";
A new variable, “val,” is defined, which can contain any string (including any HTML). You can observe that I concatenated some HTML script (which forms a TABLE) to the same variable. Now, this variable is directly written to IFRAME, which makes you to see a dynamic TABLE in an IFRAME!
Any comments, suggestions, ideas, improvements, bugs, errors, feedback etc. are highly appreciated at jag_chat@yahoo.com.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |