vunet.us@gmail.com a écrit :
Quote:
I cannot control wheather user enters text or html. if html is
entered, then would I really have to check for every possible html
tag, such as <br>, <table>, etc. to preserve plain text line breaks?
>
For example, If user enters:
-this
-is
-example
then the JS innnerHTML shows "-this -is -example", so I need to
convert line breaks to <br>.
But if user types "-this <br>-is <br>-example", there will be a break
with innerHTML.
What if situation is somehow different than this? Do i need to
consider every case or it would be just more simple to send it to
another popup window and display it the way it would really look no
matter what...?
you'll have same problems in a new window(*) or in a div in main window.
You asked about text entered as html (with tags and so on)
and that works fine in my test.
Now if you want to display in html some "normal" text it is not same song.
You can try :
function preview() {
var t = document.forms[0].userText.value;
if(t.indexOf('<')<0) t = '<pre>'+t+'<\/pre>';
var v = document.getElementById('viewer');
v.innerHTML = t;
v.style.display = 'block';
}
But probably you'll prefer :
function preview() {
var t = document.forms[0].userText.value;
t = t.replace(/>(\n|\r)/g,'>').replace(/\n|\r/g,'<br>');
var v = document.getElementById('viewer');
v.innerHTML = t;
v.style.display = 'block';
}
(*) about the option "opening in a new window", test that :
<html>
<script type="text/javascript">
function preview() {
truc = window.open('','','width=300,height=300');
truc.document.open();
truc.document.write(document.forms[0].userText.value);
truc.document.close();
}
</script>
<form>
<textarea name="userText" style="width:80%;height:200px">
<div style="background:yellow">
<h1>test</h1>
<p>to see
</div>
an now,
with some
return carriages
</textarea>
<input value="Preview" onclick="preview();" type=button>
</form>
</html>
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date