Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Javascript in Javascript

    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

    Code:
    <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??

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,882
    Thanks
    56
    Thanked 539 Times in 536 Posts
    I think this is how you do it...

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

  • Users who have thanked xelawho for this post:

    techknow (09-16-2011)

  • #3
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Perfect thanks i knew it was easy i just wasn't seeing it.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,882
    Thanks
    56
    Thanked 539 Times in 536 Posts
    hey, don't thank me. I shamelessly filched the code from Old Pedant. I don't even know how it works

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by xelawho View Post
    I think this is how you do it...

    Code:
    document.write('<sc' + 'ript type="text/javascript" '+ ' observe(x);' +'</sc' + 'ript>');
    Minor goof. Missing the > after "text/javascript"
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,882
    Thanks
    56
    Thanked 539 Times in 536 Posts
    refer to post #4

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ahhh...so now it's all my fault! Sheesh. And here I thought my boss was the only one who knew that. <grin/>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •