Click to See Complete Forum and Search --> : Displaying code in new browser window


JSBeginner
02-15-2003, 12:31 AM
Trying to figure out how to display source code of html file in new window using JS. Already have button and new window.....and that is as far as I have gotten.

pyro
02-15-2003, 12:52 AM
This should get you going:

<script language="javascript" type="text/javascript">

var newWindow = window.open("about:blank", "test", "width=600, height=400");
newWindow.document.write("<html><head><title>My test window</title></head><body>\n");
newWindow.document.write("My body text\n");
newWindow.document.write("</body></html>\n");

</script>

JSBeginner
02-15-2003, 01:18 AM
thank you -
okay - if I use this method wouldn't I need to create a function to open a new window in the script section?
How would I call this variable from within my button onclick?

rellik
02-15-2003, 09:11 AM
to display the html code, you can display it in a text box:

<html>
<head>
<script>
function buttonOpen() {
var newWindow = window.open("about :blank", "test", "width=600, height=400");
newWindow.document.write("<html><textarea rows='23' cols='70'>");
newWindow.document.write("<html><head><title>My test window</title></head><body>\n");
newWindow.document.write("My body text\n");
newWindow.document.write("</body></html>\n");
newWindow.document.write("</textarea></html>");
}
</script>
</head>
<body>
<input type="button" onClick="buttonOpen()" value="Open window">
</body>
</html>

pyro
02-15-2003, 10:01 AM
In rellik's reply, the following two bold lines should be removed. (Since they double up the <html> tag.)

<html>
<head>
<script>
function buttonOpen() {
var newWindow = window.open("about :blank", "test", "width=600, height=400");
newWindow.document.write("<html><textarea rows='23' cols='70'>");
newWindow.document.write("<html><head><title>My test window</title></head><body>\n");
newWindow.document.write("My body text\n");
newWindow.document.write("</body></html>\n");
newWindow.document.write("</textarea></html>");
}
</script>
</head>
<body>
<input type="button" onClick="buttonOpen()" value="Open window">
</body>
</html>

rellik
02-15-2003, 11:17 AM
no, infact, the lines you have emboldened are simply to define the page as a html page and the lines in between show the source of the previous page within the textarea. if it wasnt entered within a textarea, the source would be shown, it would be interpretted as a html file

pyro
02-15-2003, 11:19 AM
Oops... Didn't even see the <textarea> tag. You are of course right. Next time, I'll look closer before I post... :o