0

I have the following construct, which obviously is just HTML:

<script type="text/html" id="abc">
//... some text
</script>

Now I want to put some JavaScript into it. So it would look like:

<script type="text/html" id="abc">
//...
    <script type="text/javascript">
       alert("Hello!");
    </script>
//...
</script>

Of course, it won't work, as I would close the script-tag, before the whole code block ends.

So, is there a proper way to embed JavaScript into my script-block?

11
  • 2
    you don't need the second script tags Commented Mar 18, 2015 at 12:55
  • @ztadic91 First tags are text/html. Second are text/javascript. Second ones are required. Commented Mar 18, 2015 at 12:55
  • 1
    @ztadic91: The OP is probably trying to include scripts into some text/html templates. Commented Mar 18, 2015 at 12:55
  • @Cerbrus: Yes, that's true. Commented Mar 18, 2015 at 12:57
  • 2
    @YerkoPalma: The OP is trying to include scripts into some text/html templates. So simply leaving out the tag won't work. Commented Mar 18, 2015 at 12:58

1 Answer 1

-2

if you really want to manually create Script tag inside <script> using JavaScript... then here is a way to achieve the same:

It won't work with text/html, please try as below:

<script type="text/javascript" id="abc">
//...
    var script=document.createElement('script');
    script.type='text/javascript';
    script.src=url;

    $("body").append(script);
//...
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Nope, nope, nope. JavaScript inside <script type="text/html"> won't run.
hey @Cerbrus, answer is edited now
Please read the question again and have a look at what the OP is trying to do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.