Just trying to set up a little test page and even it doesn’t behave as expected.
Given the following working code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">
<title>Terry's Test Page</title>
<script src='jquery-1.7.1.min.js'></script>
</head>
<body>
<p>from the html in index.html</p>
</body>
</html>
I get what I expect, the one line from the html and Terry’s Test Page in the tab label. However, if I add just before the </head> line:
<script>$(document).ready(function() {
document.write('<p>from the js</p>');
});
</script>
I properly get the ‘from the js’ line — no from html since document.write replaces it — but the tab label is not filled with Terry’s Test Page. In IE the tab label shows the url, in Firefox it shows ‘Connecting …’ but it never gets beyond that.
What is it about document.write that I don’t understand? Do I have to issue some sort of end of file or some such?
Since you call
document.writeafter page load, it callsdocument.open, which empties the whole document. The browser does its best and creates a complete new document with what you provide. The document is not only the content in thebody, but everything else as well.In your case, your document is
<p>from the js</p>, but since this is not a valid HTML document (missinghtml,headandbody), the browser will add what’s necessary.For example take
This will just create an empty
body, but set the title of the page tofoo.