Forum Moderators: open
I am trying to illustrate what a snippet of javascript will look like when displayed on a webpage. It is for an assignment, and I must use innerHtml.
I am using a text area to input the javascript. I use the onChange() function for the text field to send the value of the text field to a javascript function which then sets the innerHtml of a portion of a table.
If I just type some text (not javascript) into the textbox, the function works fine and displays the text. However, it will not display the javascript code results.
Any suggestions on how to do this simply and properly?
Thanks in advance
Daniel
Sorry, maybe I wasn't so clear. Yes, I need to force the innerHtml to display the javascript code results. For example, I would need the following:
<script type="text/language>
<!--
document.writeln("Hello");
// -->
</script>
to appear in the innerHtml section as:
Hello
Right now all I get is a void space. How do I force the innerHTML to produce the results of the javascript?
Thanks again.
Daniel
var hi="Hello";
document.getElementById("outputTarget").innerHTML=hi;
My assignment specifically states that I have to allow a user to test and display their javascript code using innerHTML. But you are saying this is not possible as the innerHTML will not force the execution of the code. Is there a different solution? In the body of my HTML page, I have similar code to what you posted. Let me know what you think.
------------> Contained within body of HTML page:
<script type="text/javascript">
function updateText(){
var string = test.inputText.value;
if (document.getElementById)
document.getElementById("resultSpace").innerHTML = string;
}
</script>
<div align="left" id="resultSpace">
<script type="text/javascript">
document.writeln("Your results are displayed here.");
</script>
</div>
-------------------------> User input on web page.
<script type="text/javascript">
document.writeln("Hello");
</script>
A more direct way to achieve code execution would be to open a new window that has had the code written to it with document.write(), when the page is built. Scripts are actually only loaded when the page is loaded, though execution of functions can be made dependent on some other event.
______________
¦Input..Output ¦
¦ ______ _____ ¦
¦¦.....¦¦.....¦¦
¦¦.....¦¦.....¦¦
¦¦..X..¦¦..Y..¦¦
¦¦.....¦¦.....¦¦
¦¦_____¦¦_____¦¦
¦______________¦
I would make x a textarea so the user can edit.
And make x an iframe so u can use document.write.
Then make a script that converts the input in x
to output in y. And add a button to execute that
script and your done.
<html>
<head>
<title>HomeWork 1.0</title>
</head>
<body> <script type="text/javascript"><!--
function Txt2Frame() {
document.frames.my_frame.document.open();
document.frames.my_frame.document.write('<html><head><title>My_Frame</title></head><body>');
document.frames.my_frame.document.write(Txt2Frame.arguments[0]);
document.frames.my_frame.document.write('</body></html>');
document.frames.my_frame.document.close();
}
//--></script>
<form name="test_form">
<textarea onChange="Javascript:Txt2Frame(CharT.value);" name="CharT" cols="10" rows="5"></textarea>
</form>
<iframe frameborder="1" scrolling="0" name="my_frame" width="300" height="55"></iframe>
</body>
</html>