If there is no HTML tag after &myhtml
JavaScript or the HTML rendering engine probably interprets it as an unrecognized or incomplete entity (lacking an ending ;
) and does not render it. If you follow me&myhtml
with an HTML tag, then JavaScript or the HTML rendering engine recognizes that &myhtml
is not an incomplete entity because it is followed by an HTML tag instead of a ;
.
It doesn't matter what HTML tag you follow the string with, <a>
or <p>
work just as well as <br>
.
The behavior is not consistent across all browsers. IE 6, 7 & 8 and Firefox 2, 3 & 3.5 behave the way you describe, Safari for Windows 3 & 4 render nothing when you comment out the second document.write()
or do something like document.write("abc");
. Opera 9.64 & 10b3 render the text correctly regardless of the content of the second write()
.
Note that using document.write()
in the onload
event without writing out correctly formatted and valid HTML (complete with doctype and <html>
, <head>
, etc) is probably a bug anyway. Note the problem does not manifest itself if you do:
<html>
<head>
<script>
function delimit() {
document.write('me&myhtml');
}
</script>
</head>
<body>
<script>
delimit();
</script>
</body>
</html>
To answer your question, it is a bug in either the implementation of a specification, or an undefined part of a specification that each browser implements differently. What you are doing is slightly incorrect, the outcome of that slightly incorrect code is not defined and it can not be relied on to behave consistently across all of the major browsers in use today. What you are doing should be avoided.