How to put a script inside another

1

This code is to replace the contents of a div, and I need to add this script inside the code but I'm not getting it.

window.onload = function substituir() {
  document.getElementById("fgh").innerHTML = "<div class="dmg"><script type="text/javascript" src="//ylx-1.com/bnr.php?section=post&pub=524113&format=468x60&ga=g"/>\n<noscript><a href="https://yllix.com/publishers/524113" target="_blank"><img src="//ylx-aff.advertica-cdn.com/pub/468x60.png" style="border:none;margin:0;padding:0;vertical-align:baseline;" /></a></noscript></div>"
}
<div id='fgh'></div>
    
asked by anonymous 29.07.2018 / 18:45

1 answer

1

You have 2 basic code problems:

1st As mentioned, you are using double quotation marks as the string's delimiter and within itself, without making the escapes ( \" ) in the inside quotes. This causes the string to break, resulting in an error. In this case, instead of making escapes, simply delimit the string with single quotation marks ( ' ).

When creating a string where there will be double quotation marks inside, or you use double quotation marks as a delimiter and within single quotation marks or vice versa, not both at the same time:

// aspas simples delimitando a string
// e aspas duplas internas
var string = '<div id="minhadiv"></div>';

or

// aspas duplas delimitando a string 
// e aspas simples internas
var string = "<div id='minhadiv'></div>";

2nd You are wanting to insert the <noscript> tag from a script .

The noscript tag is just to detect if the browser does not support scripts (or is disabled in the settings). It does not make sense to insert this tag from a script, because if you can enter it is because the browser runs scripts and the tag will not have any effect. Not to mention you're still trying to insert the tag inside another script, which is already wrong. The tag should be inserted directly into the HTML and outside the <script></script> tag, something like:

<noscript>
   Seu navegador não suporta scripts ou está desativado
</noscript>
<script>
   // códigos
</script>
  

For the title of the question, what is between <noscript></noscript> is not a script.

I suggest reading this documentation about the <noscript> tag.

    
29.07.2018 / 20:43