...

View Full Version : Javascript in Javascript



techknow
09-16-2011, 05:05 AM
Hey guys I know this might have been discussed before but i can't find it anywhere. I'm trying to write javascript with javascript.... simple right well the problem is in my script i have javascript writing the </script> line like this



<script type="text/javascript">
if (!Array.prototype.forEach)
{
Array.prototype.forEach = function(fun, thisp)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();

var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
}
var typeArray = function (x) {
document.writeln('<script type="text/javascript">observe(x);</script>');
}

var myArray = [1,2,3,4,5];

myArray.forEach(typeArray);
</script>
When this code is ran it ends at the first </script> tag and you see all the code after it as just text.

i need this code to work because the function observe(); is an observer and has to be in the code for each instance of x. but when the </script> tag is written, even inside the "" it ends the code. Is there any way to write the </script> tag inside of javascript without ending the code??

xelawho
09-16-2011, 05:24 AM
I think this is how you do it...


document.write('<sc' + 'ript type="text/javascript" '+ ' observe(x);' +'</sc' + 'ript>');

techknow
09-17-2011, 12:23 AM
Perfect thanks i knew it was easy i just wasn't seeing it.

xelawho
09-17-2011, 12:39 AM
hey, don't thank me. I shamelessly filched the code from Old Pedant. I don't even know how it works :eek:

Old Pedant
09-17-2011, 12:42 AM
I think this is how you do it...


document.write('<sc' + 'ript type="text/javascript" '+ ' observe(x);' +'</sc' + 'ript>');

Minor goof. Missing the > after "text/javascript"

xelawho
09-17-2011, 12:44 AM
refer to post #4 :o

Old Pedant
09-17-2011, 12:47 AM
Ahhh...so now it's all my fault! Sheesh. And here I thought my boss was the only one who knew that. <grin/>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum