Consider the following:
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
document.write('<script type="text/javascript" src="script2.js"></scr'+'ipt>');
document.write('<script type="text/javascript" src="script3.js"></scr'+'ipt>');
console.log(document.getElementsByTagName("script").length + " Scripts");
console.log(document.head.innerHTML);
</script>
</head>
<body>
</body>
</html>
What would you expect the console.log to contain? I hope you come to one of the two outcomes that I would expect: 4 Scripts
and either the two existing or all four script tags shown in the head's innerHTML (document.write
could as well write to the body, so one could expect the script tags to be injected as children of the body).
The thing is that, in Chrome and IE11, the first script tag added via document.write is shown in the head's innerHTML
, but the second isn't, and the DOM query result is 3 Scripts
.
Could anyone please elaborate?