tags:

views:

29

answers:

1

I am creating some JSON on the fly, serializing it and saving it to the DB. To run it, i create a script element, and load it that way. Is there a way to load the script source to a textarea?

+1  A: 

You don't "run" JSON, JSON is a data notation.

Yes, you can put the text of a script tag into a text area, something along these lines:

var script, tb, node;
script = document.getElementById('theScriptID');
tb = document.getElementById('theTextBoxID');
tb.innerHTML = script.innerHTML;

Example:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
</style>
<script id='theScriptID' type='text/javascript'>

    function go() {
        var script, tb;
        script = document.getElementById('theScriptID');
        tb = document.getElementById('theTextBoxID');
        tb.innerHTML = script.innerHTML;
    }

</script>
</head>
<body>
<textarea id='theTextBoxID' rows='20' cols='70'></textarea>
<input type='button' id='btnGo' value='Go' onclick="return go();">
</body>
</html>
T.J. Crowder