"Node was not found" error

Y

yawnmoth

When I visit the following webpage:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
</head>

<body>
<div id="demo">this is a test...</div>
<script>
result = document.createElement("span");
result.style.color = "#f00";
result.style.fontWeight = "bold";
result.appendChild(document.createTextNode(" ...that has been a
success"));

var demo = document.getElementById("demo");

demo.parentNode.replaceChild(demo,result);
</script>
</body>

I get this error (in FF2):

uncaught exception: [Exception... "Node was not found" code: "8"
nsresult: "0x80530008
(NS_ERROR_DOM_NOT_FOUND_ERR)" location:
"http://www.domain.tld/test.html Line: 17"]
 
R

Randy Webb

David Golightly said the following on 11/9/2006 5:39 PM:

Also I would discourage you from using script tags within the <body>
section of your document, unless you have a good reason to do so (and
there aren't many). Put the <script> tag in the <head> section and
wrap your code in a window.onload handler, like this:

<script>
window.onload = function() {
var something = document.getElementById('something');
// ... code goes here
}
</script>

And you call that "good, modern Javascript style" to leave out the type
attribute (which is mandatory)? No feature detection. Hmmm.

Also, could you please explain how to wrap code in a function that uses
document.write to add advertisement to pages and fire it onload without
wiping out the page?

Check out http://www.onlinetools.org/articles/unobtrusivejavascript/
for some good, modern Javascript style.

If the free chapter is indicative of the entire book, no thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top