0

I'm having trouble inserting this code into my HTML page:

<script type="text/javascript">
var1 = "1";
var2 = "2";
var3 = "3";
</script>
<script type="text/javascript" src="http://www.someadress.com/sss.js"></script>
<noscript><a href="http://someadress" target="_blank"><img src="someimg" width="468"
height="60"></a></noscript>';

I've tried using innerhtml, but it just doesn't seem to work. Here is an example of what i have tried; I'm trying to add the script inside the element with ID allImages:

    var imagesHTML = document.getElementById("allImages").innerHTML;
    imagesHTML = '<script type="text/javascript">
    var1 = "1";
    var2 = "2";
    var3 = "3";
    </script>
    <script type="text/javascript" src="http://www.someadress.com/sss.js"></script>
    <noscript><a href="http://someadress" target="_blank"><img src="someimg" width="468"
    height="60"></a></noscript>';
0

1 Answer 1

0

You cannot load <script> tags by injecting HTML. Instead, you need to use eval() functions! There are other ways of creating DOM Objects and invoking scripts. Please check out these:

Note: You should never use eval(), unless you know the environment and is absolutely necessary.

If you don't need the hassle of these things, you can always use libraries like jQuery and MooTools to insert HTML using .html() function.

Sign up to request clarification or add additional context in comments.

5 Comments

"Instead, you need to use eval() functions!" No, you don't. And arguably really shouldn't.
Separately: Since you know this is a duplicate of a previous question, even going to the trouble to link to it, why not close the question as a duplicate?
@T.J.Crowder Updated the answer. Please do check it out.
"Instead, you need to use eval() functions!" is still there, and still incorrect.
the duplicated answer dosent seem to aim to 1 good solution+ i'm having trouble understanding the code,i use the script for the whole html or each time for diffrent tags?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.