The stylesheet is ignored when using bodyload and document.write

I fiddled with JavaScript, experimenting to feel it, and already ran into a problem. Here is my html code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript" src="testing.js"></script>
</head>
<body onload="writeLine()">
</body>
</html>

Here is the JavaScript testing.js:

function writeLine()
{
    document.write("Hello World!")
}

Here is the styles.css stylesheet:

html, body {
    background-color: red;
}

So, a very simple example, but I may have chosen an inconvenient example using on-load in the body tag. So the code above loads and runs the function, but the stylesheet does nothing unless I remove the script tags in my head. I tried putting script tags everywhere but nothing works. I researched online how to link to JavaScript files correctly, and did not find a clear solution, can anyone point out my error?

I have used JavaScript before, but I want to get a clear understanding from the very beginning, before using it anymore

+5
3

document.write(), *. , . , (undefined?)

dom :

function writeLine() {
    var text = document.createTextNode("Hello World!");
    document.body.appendChild(text);
}

Opera , . document.write() - . , <script> body, .

*, , document.write(),

+2

document.write ( onload), ( ).

DOM, 8 9 W3C JavaScript Core Skills.

+3

, script , document.write , , .

If you add a warning before overwriting the contents, you can see that your page is red before it is overwritten with Hello World.

+2
source

All Articles