www.webdeveloper.com
Results 1 to 5 of 5

Thread: Javascript source code in variable

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Javascript source code in variable

    I'm trying to add some javascript source code into a textarea dynamically but it's not letting me set a variable with the source code.

    I have the following code:
    Code:
    <script type="text/javascript">
    function getEmbedCode(inKey){
    	showEmbedCodeDialog();
    	embedCode = '<script type="text/javascript></script>';
    	$('#embedCode').html(embedCode);
    	
    }
    </script>
    The </script> acts as an actual script end and doesn't allow the variable to have that in it.

    Any suggestions?

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,126
    You have to make the embedCode a jQuery object instead of a string.

    Try this:

    Code:
    <script type="text/javascript">
    function getEmbedCode(inKey){
    	showEmbedCodeDialog();
    	embedCode = $('<script type="text/javascript" />');
    	$('#embedCode').html(embedCode);
    	
    }
    </script>
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Oct 2012
    Posts
    9
    Or you can also escape the slash, like so:
    Code:
    	embedCode = '<script type="text/javascript><\/script>';

  4. #4
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,126
    Quote Originally Posted by bundat View Post
    Or you can also escape the slash
    I don't believe that's the problem. In your example, embedCode is still acting as a string. The next line, he's trying to inject HTML into a string, which won't work.
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  5. #5
    Join Date
    Oct 2012
    Posts
    9
    He said he wants "source code" to be generated into a textarea, so I assume he wants it as a string, NOT as a script element.

    You actually can't have </script> in a string unescaped in a lot of browsers, try it. This is the solution to that problem.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles