PDA

View Full Version : Preserving line breaks from textarea in javascript preview



clj83
04-02-2009, 01:09 PM
Hey,
I am using this code to create a preview pop up window of some text entered into a form textarea. I cannot get it to preserve the line breaks though. Any help would be greatly appreciated!

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Preview</title>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript">

function PreviewText()
{

var generator=window.open('','preview','height=400,width=500');

generator.document.write('<html><head><title>Preview text</title>');
generator.document.write('</head><body>');
generator.document.write(document.forms[0].txt.value);
generator.document.write('</body></html>');
generator.document.close();

}

</script>
</head>
<body>
<h1>Preview in javascript</h1>
<form method="post" action="">
<textarea rows="10" cols="50" name="txt"></textarea>

<input type="button" onclick="javascript:PreviewText();return false;" value="Preview" />
</form>

</body>
</html>

Thanks