Forum Moderators: phranque

Message Too Old, No Replies

JavaScript document.write within a Frame

How do I write in a frame using document.write?

         

The_Warden

5:41 pm on May 13, 2003 (gmt 0)

10+ Year Member



Hi. I have a situation where I'm unfortunately using frames. Now I want to use JavaScript, document.write to output a value within a frame. The problem is every time document.write is executed the entire frame is overwritten and just contains the value. Correct me here if I'm wrong but it is possible to document.write in a frame without the whole document been over written?

Here's a basic example of what I'm doing. Any help would be greatly appreciated!

index.html
<HTML><HEAD><TITLE>Venmar's Intranet</TITLE>
<LINK REL="icon" HREF="/favicon.png" TYPE="image/png">
<LINK REL="shortcut icon" HREF="/favicon.ico">
</HEAD>

<FRAMESET ROWS="65,*" FRAMESPACING="0" FRAMEBORDER="1" BORDER="1">
<FRAME FRAMEBORDER="1" SCROLLING="no" MARGINWIDTH="0" MARGINHEIGHT="0" NORESIZE SRC="navbar.html" NAME="navbar">
<FRAME FRAMEBORDER="1" SCROLLING="auto" MARGINWIDTH="0" MARGINHEIGHT="0" NORESIZE SRC="body.html" NAME="main">

<NOFRAMES>
Your browser doesn't support frames. Please upgrade your browser to current release.
</NOFRAMES>
</FRAMESET>
</HTML>

navbar.html
hello

body.html
hello

<SCRIPT LANGUAGE="JavaScript" TTYPE="text/javascript">
function fnTest()
{
document.write("hello world");
}
</SCRIPT>
<FORM>
<INPUT TYPE="text" NAME="test" onChange="fnTest()">
</FORM>

BlobFisk

6:10 pm on May 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



document.write (and document.wroteln) is usually invoked as the page is loaded. If it is invoked after the page has loaded (as it seems in your case), it clears the current document, opens a new output stream, and writes what you have asked it to to the frame. In essence, destroying your frame.

To do something like this after the page has loaded you will need to write it to an object that can change, like an input field.

So, for example:



<script type="text/javascript">
function fnTest()
{
document.form1.bob.value='hello world';
}
</script>

<form name="form1">
<input type="text" onChange="fnTest()">
<input type="text" name="bob" readonly>
</form>


HTH

BlobFisk

6:22 pm on May 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Apologies, The_Warden,

On reflection there is a way that you could do this, using the javascript innerText method.


<form name="form1">
<input type="text" onChange="fnTest2()">
</form>

<script type="text/javascript"> 
function fnTest2()
{
document.getElementById('daLayer').innerText='hello world';
}
</script>

<div id="daLayer">&nbsp;</div> 

This will then populate the layer, daLayer, with the text that you have specified.

Caveat: This will only work in IE and Opera.

The_Warden

7:53 pm on May 13, 2003 (gmt 0)

10+ Year Member



Thanks for the reply! Sounds like you are saying the second example will only work in IE and Opera. Is there any way to do this to have it work in Netscape 6+/Mozilla? Is this problem a fact of it not working in Netscape 6+/Mozilla because of a bug or lack of current support?

My example code above was a general idea to allow the client to open a document after they provided a path/file name. I've resolved this using a button. Thanks.

Now I still want to do the same thing again with document.write (as I stated in my example above), but this time based on a form field to toggle text color between two colors. This would depend on if the value of another form field is disabled/readonly or not disabled/readonly (true/false). Can this be done doing JavaScript?

BlobFisk

8:09 am on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Is this problem a fact of it not working in Netscape 6+/Mozilla because of a bug or lack of current support?

Your second assumption is correct - they just don't support it at this time.


but this time based on a form field to toggle text color between two colors.

This is slightly easier to achieve. You could wrap the text in question in a span, give that span an ID and then change the style property of that ID using:

document.getElementById.style.color='blue';

Again, this is DOM1 compliant only, you would need to add some conditionals in to deal with older browsers.