Add script tag line after previous script indetend

I use the following code to add a script tag after an existing nested script tag (adding a new script tag inside the html file by code)

return '\n' + "<script>\n" + '\t' + scriptContent + "\n</script>\n"; 

(Script content is helloworld

Html content is as follows

enter image description here

I want the second open script tag to be the line after the script tag (in the first line), how can I do this? the first "\ n" does not help here ...

It just works when I add br , how can I remove this exact br , if that is the only solution I have here ...

 return "<br />" +"\n" +"<script>\n" + '\t' + scriptContent + "\n</script>\n"; 

Please take a look at the following js fiddle: the script tag is not indented after you click on convert ...

please take a look at the following jsFiddle (similar to what I used, which I took from SO as a reference)

Jsfiddle

jsfiddle.net/rd0mn3wh/1

as you can see, when you click to convert the script object where there is no indentation (new script and existing script), how can I handle this, or maybe there is a better way to do this?

I want a new script to be added as follows (script with alerty inside ...

 .... ui-resourceroots="{'tdrun': './'}"> </script> <script> alert("test"); </script> 

I need the following:

  • Insert a new script after providing the script ID (in the code after test-ui-bootstrap ')

  • Update attribute value (when

    key provided)

Both of the answers below do not help here ...

+5
source share
3 answers

The problem is that .insertAfter() trims the leading white space from your HTML line. The solution is to use .after() instead. I realized this while playing with my violin, but the updated violin.

+9
source

The problem is where this returned string will be processed.

\n , \r , \t etc. - all JavaScript string escape codes and, as such, they will only work when the string is evaluated by the JavaScript runtime. If you have a JavaScript string containing these codes and passing this string to an HTML parser (for example, via .innerHTML ), then the HTML parser will ask you to evaluate this string in the HTML parser world as well <br> how to add a string, not \n . That is why <br> working for you.

So, when \n is evaluated by the JavaScript engine, a newline character is entered into the string, but when the HTML parser receives this newline character, it simply ignores it, as it would with any carriage return in the markup.

In the old days, we could get this β€œpretty” code by typing it with document.writeln() , which will write your content, and then add the line feed to the HTML. You can still do it today, but this is not considered good practice because it requires you to embed the code inside the HTML document, otherwise you will overwrite the entire DOM.

If this is what you just want, because it will make you feel warm and fuzzy, I would recommend forgetting about it and moving on.

Finally, here is the standard DOM method for creating new elements and entering them in the DOM:

 var o = document.getElementById("output"); var scriptContent = "alert('Hello World!')"; var s = document.createElement("script"); var t = document.createTextNode(scriptContent); s.appendChild(t); o.appendChild(s); 
 <div id="output"> </div> 
+4
source

How about this:

 return "\n<script>\n\t" + scriptContent + "\n</script>\n"; 
0
source

Source: https://habr.com/ru/post/1246474/


All Articles