How to put a script inside another script

Asked

Viewed 207 times

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>
  • 2

    "within the code"? What do you mean by that? And you properly escaped the quotation marks? The way it is in the question does not work, because the quotes are not properly escaped, breaking the code and leaving the syntax invalid.

  • That’s the doubt I have... if you can explain to me what I should do I would be very grateful!

  • 2

    Is your code exactly as mentioned? Because if it is, your problem is with quotation marks. Replace double quotes involving the string to be inserted with single quotes. innerHTML = ' <div class="dmg">... '

1 answer

1


There are 2 basic problems in the code:

first. As mentioned, you are using double quotes as the delimiter of the string and within it without making the proper escapes (\") inside quotes. This causes the string to break, resulting in error. In this case, instead of making escapes, just delimit the string by single quotes (').

When creating a string there will be quotes inside, or you use double quotes as delimiter and inside single quotes 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’re wanting to insert the tag <noscript> from a script.

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

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

By the title of the question, whatever is between <noscript></noscript> is not script.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.