JavaScript document.createElement not showing

I wonder why this code works and is displayed in the source, and the other code does not work?

Code Display:

function test() { var scriptElement2 = document.createElement("script"); scriptElement2.id = 'test'; var head = document.getElementsByTagName("head")[0] || document.documentElement; head.appendChild(scriptElement2); } test(); 

Code not displayed:

 function test() { var scriptElement2 = document.createElement("script"); scriptElement2.id = 'test'; scriptElement2.src = 'http://test.com/ja.js'; scriptElement2.type = "application/javascript"; var head = document.getElementsByTagName("head")[0] || document.documentElement; head.appendChild(scriptElement2); } test(); 

I added only two lines:

 scriptElement2.src = 'http://test.com/ja.js'; scriptElement2.type = "application/javascript"; 

Now the code no longer works, it does not appear in the HTML source code. And yes, I tried it with valid src.

+4
source share
1 answer

Try setting the type text/javascript or leaving it at all.

0
source

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


All Articles