Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stylesheet ignored when using body onload and document.write

Tags:

javascript

I am messing around with JavaScript experimenting to get a feel for it and have already hit 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 style sheet styles.css:

html, body {
    background-color: red;
}

So a very simple example, but I may have chose an awkward example, using on-load in a body tag. So the code above loads and runs the function, but the style sheet does nothing, unless I remove the script tags in the head. I have tried putting the script tags everywhere else, but nothing works. I have researched on-line how to properly link to JavaScript files, and have no found no clear solution, can anyone point out my error?

I have used JavaScript before, but I want a clear understanding from the beginning before I use it any longer

like image 361
deucalion0 Avatar asked May 28 '12 13:05

deucalion0


3 Answers

You cannot use document.write after the document is closed (which it will be when onload fires) without destroying the existing document (including links to stylesheets).

Instead, use DOM manipulation, which is covered by chapters 8 and 9 of the W3C JavaScript Core Skills.

like image 189
Quentin Avatar answered Sep 20 '22 01:09

Quentin


Your problem is with the document.write() called in a wrong moment*. This method prints given text at current place in the page as was intended to work while the page still loads. Because you are calling it when the whole page was loaded, the results are unexpected (undefined?)

Instead you should manipulate the dom tree directly:

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

Actually in Opera browser I see red background for few milliseconds and then it goes back to white. Try commenting out document.write() - the background is as expected. Moreover you should include <script> tag at the end of body, but this won't solve your problem.

* to be honest, there is no good moment for calling document.write(), avoid it

like image 33
Tomasz Nurkiewicz Avatar answered Sep 20 '22 01:09

Tomasz Nurkiewicz


In your particular example it doesn't matter where the script tag is added as the document.write command executes after the content is rendered, overwriting the existing content.

If you add an alert before overwriting the content you can see your page is red before it gets overwritten with Hello World.

like image 45
Nope Avatar answered Sep 21 '22 01:09

Nope