I using the following code to add script tag after existing enclosing script tag (adding new script tag inside html file by code)
return '\n' + "<script>\n" + '\t' + scriptContent + "\n</script>\n";
(Script content is helloworld
The html content look like following
I want that the second opening script tag will be line after the enclosing script tag(in the first line) how can i do that ? the first '\n' doesnt help here ...
It just working when I add br, how can I remove this exact br if this is the only solution that i've here...
return "<br />" +"\n" +"<script>\n" + '\t' + scriptContent + "\n</script>\n";
Please have a look at the following js fiddle the both script tag are not indented after you click on convert...
please have a look at the following jsFiddle(similar for what I use which took from SO as reference )
JSFiddle
as you can see when you click on convert both script object where not indented (the new script and the existing script), how can I handle it,or maybe there is better way to do it ?
I want that the new script will be add like following( the script with the alerty inside...
.... ui-resourceroots="{'tdrun': './'}"> </script> <script> alert("test"); </script>
I need the following:
Insert a new script after provided script ID(in the code after test-ui-bootstrap')
update attribute value (when
the key is provided)
Both below answers are doesn't help here...
3 Answers
Answers 1
The problem is that .insertAfter()
is trimming the leading white-space from your HTML string. The solution is to use .after()
instead. I figured this out by playing with your fiddle and here is the updated fiddle.
Answers 2
The issue has to do with where that returned string is going to be processed.
\n
, \r
, \t
, etc. are 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 that contains these codes and pass that string to the HTML parser (via .innerHTML
, for example), then the HTML parser will be asked to evaluate that string, and in the HTML parser's world, <br>
is how you add a line feed, not \n
. That's why the <br>
is working for you.
So, when the \n
is evaluated by the JavaScript engine, a newline character is injected into the string, but when the HTML parser gets that newline character, it just ignores it, as it would with any carriage return in markup.
In the old days, we could get this "pretty" code by injecting it with document.writeln()
, which would write your content and then add a line feed into the HTML. You can still do this today, but it is not considered a good practice because it requires that you inject the code inline within the HTML document, otherwise you will overwrite the entire DOM.
If this is something you just want because it will make you feel warm and fuzzy, I'd recommend forgetting about it and moving on.
Finally, here is the DOM standard way of creating new elements and injecting them into 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>
Answers 3
How about this :
return "\n<script>\n\t" + scriptContent + "\n</script>\n";
0 comments:
Post a Comment