You could just get the div element you want the code to be in, and then set the innerhtml to <script>your code here</script>
EDIT:
Try assigning your function like so:
var myFunc = function() {
//Code
}
Then you can call myFunc(parameters)
3
solved How to add local variables and a different function in script element in Javascript [closed]