469,282 Members | 1,889 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

"Node was not found" error

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"]

Nov 9 '06 #1
2 14153

yawnmoth wrote:
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"]
Take a closer look at this:

http://developer.mozilla.org/en/docs...t.replaceChild

The parameters should be replaceChild(new, old) and you've got
replaceChild(old, new).

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 <scripttag in the <headsection and
wrap your code in a window.onload handler, like this:

<head>
<title>Test</title>
<script>
window.onload = function() {
var something = document.getElementById('something');
// ... code goes here
}
</script>
</head>
<body>
<div id="something"></div>
</body>
</html>

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

Nov 9 '06 #2
David Golightly said the following on 11/9/2006 5:39 PM:

<snip>
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 <scripttag in the <headsection and
wrap your code in a window.onload handler, like this:
<snip>
<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?

<snip>
Check out http://www.onlinetools.org/articles/...ivejavascript/
for some good, modern Javascript style.
If the free chapter is indicative of the entire book, no thanks.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Ari Royce Hidayat | last post: by
1 post views Thread by Casper Stendal | last post: by
8 posts views Thread by webgenius | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.