document.write() Method

document.write() method takes a HTML content to write to the window or frame. After a page loads any document.write() method called in curent page will erase the content page, and opens a new data stream. when you press "the same window" button document.write() method will erase the page and write another content. If you press "The other window" different page will be displayed. Here is an example.

Java Script Code:
In header we define a function to write new content to current window:
<script language="JavaScript">
<!--
function SameW() {
	var s = "<HTML><HEAD><TITLE>New document </TITLE></HEAD></HTML>"
	s+="<BODY  leftmargin=\"20\"><h1>This is a new document generated by <code><font
	color=\"navy\">document.write()</font></code> Method</h1>"
	s+="If you want to go back to previous page press back button on your browser."
	s+="</BODY></HTML>"
	document.write(s)
	document.close()
}
//-->
</script>

In body we call this function:
<FORM>
<INPUT TYPE = "button" VALUE = "The same window" onClick = "SameW()">
</FORM>

In header we define a function to write new content to new window:
<script language="JavaScript">
<!--
var secWindow 
function openW(){
	 secWindow = window.open("","","status HEIGHT = 400, WIDTH=600")
}
function OtherW() {
	openW()
	//Make secWindow active
	secWindow.focus()
	var s = "<HTML><HEAD><TITLE>New document </TITLE></HEAD></HTML>"
	s+="<BODY  leftmargin=\"20\"><h1>This is a new document generated by <code><font
	color=\"navy\">document.write()</font></code> Method which is dislpayed in new window</h1>"
	s+="This is a new document generated by <code><font color=\"navy\">document.write()</font></code>
	Method which is dislpayed in new window."
	s+="</BODY></HTML>"
	secWindow.document.write(s)
}
//-->
</script>

In body we call this function:
<FORM>
<INPUT TYPE = "button" VALUE = "The other window" onClick = "OtherW()">
</FORM>

Back to Java script list


Back to main page