Reputation: 7
I am a bit new to Javascript. I am writing this dummy script to test out what I need for my larger page. I want, at the simplest, to get the value of href and then change the value of href and then see it again. TO keep it simple, right now I have removed the part for changing the href value and just calling the attribute value twice. But the second time I access the attribute value it show an error which I have posted below. I'm posting two error logs one from Firebug and one from Dragonfly(from Opera).
Any help would be dearly appreciated. Thanks.
One more issue I have with the script is that it never finishes loading. I always see (in Firefox 3.6.8) the little loading sign going round and round in the tab title. It doesn't bother me so much but if anyone has an idea please tell me.
<!-- This file is used to set attribute value for href. -->
<html>
<head>
<script type="text/javascript">
function hrefvalue()
{
var thisthang = document.getElementById("1").childNodes[0].getAttribute("href");
document.write(thisthang);
document.write("\n");
var newnew21 = document.getElementById("1").childNodes[0].getAttribute("href");
document.write(newnew21);
}
</script>
</head>
<body>
<div id="1"><a href="focusdude.htm">click here</a></div>
<button type="button" onclick="hrefvalue()">Click me instead</button>
</body>
</html>
Error logs :
1. Firebug -> document.getElementById("1") is null
2. Dragonfly ->
Uncaught exception: TypeError: Cannot convert 'document.getElementById("1")' to object
Error thrown at line 8, column 0 in hrefvalue() in
file://localhost/D:/Chicago%20pics/website%20pics/website%20root/trial5.htm:
var newnew21 = document.getElementById("1").childNodes[0].getAttribute("href");
called from line 1, column 0 in (event):
hrefvalue()
And thanks again for all the fish :D
Upvotes: 0
Views: 4091
Reputation: 944203
document.write
will trash a document that isn't open. When the browser parses </html>
the document is closed. Since the function runs onclick
, it will be closed before you try to write. Use DOM methods to modify HTML with JS.Upvotes: 3