Forum Moderators: open

Message Too Old, No Replies

need help with innerHTML

Trying to create a page that will help me test code easily

         

StephenMichael

6:20 am on May 2, 2007 (gmt 0)

10+ Year Member



I have written the following page:

<html>
<head>
<title>Untitled Page</title>
<script type="text/javascript">

function testit(){
var x1=document.getElementById('testbed')
var x2=document.getElementById('testtext')
x1.innerHTML=x2.value
}
</script>
</head>
<body>
<div id="testbed" style="height:350px; width:800px;"></div>
<div>
<textarea id="testtext" style="height:150px; width:800px" onkeyup="testit()"></textarea>
</div>
</body>
</html>

It works great for testing HTML, unfortunately I rarely (if ever) need to test HTML. I wrote it so I could easily check scripts, but IE does not load scripts in innerHTML, does anyone know a work around?

mrhoo

3:37 pm on May 3, 2007 (gmt 0)

10+ Year Member



You can use the xmlHTTPRequest to get the text of a script file and insert it in a textarea:
function scriptToTextarea(url){
var ajax;
if (window.XMLHttpRequest) ajax= new XMLHttpRequest();
else if (window.ActiveXObject){
try{
ajax= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(er){
ajax=false;
}
}
try{
ajax.open('GET', url, true);
ajax.onreadystatechange= function(){
if(ajax.readyState== 4 && ajax.status== 200){
var t= ajax.responseText;
document.getElementsByTagName('textarea')[0].value= t;
}
}
ajax.send(null);
}
catch (er){
throw er;
}
}

StephenMichael

7:47 am on May 4, 2007 (gmt 0)

10+ Year Member



That does not look quite like what I am looking for. I have a DIV and a TEXTAREA in my page. When something is typed into the textarea it is converted into html in the div. It works fine unless you add now javascript. I need a way to load script written into a page with innerHTML or something similar.

vinay9955

7:57 am on May 4, 2007 (gmt 0)

10+ Year Member



ya i see .

normally textarea has the attribute "defaultValue " which sets the default value of the textarea.
so try :

function testit(){
var x1=document.getElementById('testbed')
var x2=document.getElementById('testtext')
x1.innerHTML=x2.defaultValue
}

kaled

10:10 am on May 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may be able to test scripts with eval(), however, working without IE is not a big problem. (You'll have to be careful with quotes and there may be other limitations.)

Whilst it is common for scripts that work in IE to fail in Mozilla (due to the use of non-standard stuff) most scripts that work in Mozilla work in IE.

Kaled.