473,385 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem adding node in IE6 on page load

I am having a problem adding a node to the page. This works on other
browser (Opera, Firefox, etc.), but not in IE. The following is the
function to add the node:

[---------------------------------------------------------
function add_div(e) {
//Add div node
var inputBox = document.getElementById('test');
var parent = inputBox.parentNode;

var test_div = document.createElement('div');
test_div.id = 'test_div';

var test_div_tn = document.createTextNode('This is a test');
test_div.appendChild(test_div_tn);

parent.insertBefore(test_div, inputBox);
}//end function add_div
---------------------------------------------------------]

(the id "test_div" is a defined style)

Now, the interesting thing is that it works if I use the function in a
link using "onClick='add_div()'" or "href='javascript: add_div();", but
it does not work when I try to call the function when the page loads.
I've tried just putting the code (that is in the add_div function) at
the beginning of the javascript and calling it explicitly that way.
Now, it actually does work when i put an "onLoad='add_div()';" in the
body tag. But I don't the user to have to put this in manually. So I
tried setting body onLoad equal to add_div, and ran into the same sort
of problems. I've also tried using event listeners (which is what I
normally use). The event listener code is the following:

[----------------------------------------------------------
// Cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
// By Scott Andrew
function addEvent(elm, evType, fn, useCapture) {
if (elm.addEventListener) {
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
} else {
elm['on' + evType] = fn;
}
}// end function addEvent
--------------------------------------------------------------]

and then i would add the following function call.

[------------------------------------------------------
addEvent(window, 'load', add_div, false);
--------------------------------------------------------]

The javascript error I get is either "document.getElementById(...) has
no properties" or "Unknown Runtime Error".

I've been wracking my brain for quite a long time now, and I'd
appreciate if anybody had any kind of insight into this problem.
Thanks!

Nov 23 '05 #1
1 1534
>Now, the interesting thing is that it works if I use the function in a
link using "onClick='add_div()'" or "href='javascript: add_div();", but
it does not work when I try to call the function when the page loads.


If you have:

<body>
<script type="text/javascript">
document.getElementById('abc').innerHTML = "goodbye";
</script>
<div id="abc">hello</div>
</body>

Then you will see "hello" displayed and get a javascript error that
document.getElementById... has no properties. This sounds like the
problem you are having.
HOWEVER...

If you have
<body>
<div id="abc">hello</div>
<script type="text/javascript">
document.getElementById('abc').innerHTML = "goodbye";
</script>
</body>

Then you will see "goodbye" displayed and will get no error.

If you use <body onload="">, the onload is triggered AFTER the page is
fully loaded, not when the <body> tag is hit. But inline javascript is
executed as the document is parsed.

You have to define the element first, THEN you can act on it.

Nov 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
2
by: Keith M | last post by:
Hi, I have found a very useful piece of code in the msdn which shows how to load an xml config file, search for a particular value and replace it. Could someone possibly convert it to C# from...
6
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone...
2
by: Mark | last post by:
I got this code from the Internet. Demo on the site works fine but downloaded version is not. I am a very new to .NET, I cannot figure this out, please help. Here is the code: ASPX code: <%@...
1
by: John Wilhelm | last post by:
I'm having a problem in by VB.net 2005 application. When i try to get a node from my app.config file the node come back with "nothing". The xmldocment loads OK, but I can't retrive a node. The...
2
by: Claus | last post by:
Hello, I have a long treeview with scrollbars. When I scroll down and press a treeview node, then the Load event fires and navigate in an iframe to another page. The problem is, that afterwards...
0
by: empiresolutions | last post by:
Howdy, I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set...
1
by: empiresolutions | last post by:
Howdy, I have an PHP page that edits XML files. I want ADD a new *id* attribute to all nodes on the page that do not have it all ready. Then i want to delete all of the values of *id* and set...
1
by: hemantkamb | last post by:
Hi…. I have created one website with Master and content page . I have taken TreeView Control on master page. And I wants that when I click to tree node the value of tree node is added to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.