window.onload() set element value that has not been loaded?

L

Liming

Hi,

Here is my situation. I have a textarea on the page. When the page
loads, I need it to have some default text (which will be generated
dynamically)

so I did something like this

function init()
{
document.getElementById("TexareaID").value = "default text";
}

window.onload
{
init();
}


but the problem is firefox always return "TextAreaID" has no properties
because the textbox hasn't loaded it yet..it seems. How do I get around
that? (I know mabye i can insert the script below the textbox.. but
that's ugly. Any other ways? Thanks

Liming
 
R

RobG

Liming said:
Hi,

Here is my situation. I have a textarea on the page. When the page
loads, I need it to have some default text (which will be generated
dynamically)

so I did something like this

function init()
{
document.getElementById("TexareaID").value = "default text";
}

window.onload
{
init();
}


but the problem is firefox always return "TextAreaID" has no properties
because the textbox hasn't loaded it yet..it seems. How do I get around
that? (I know mabye i can insert the script below the textbox.. but
that's ugly. Any other ways? Thanks

1. Be accepting of the ugliness - beauty fades but dumb is forever

2. Use <body onload="init()"

3. Assign init to window.onload in your script element:

window.onload = init; // Note there are no brackets "()"
 
L

Liming

Rob,

Thanks a lot. I did the third way and it works. If you dont' mind.. how
come window.onload =init works and window.onload{ init(); } is not? Is
there anything speical about it? Sorry, kinda a stupid question,
obvioulsy in this case it made a differnece, i'm just curious if there
are other situations where these two will be different.

Thanks a lot.

Liming
 
R

RobG

Liming said:
Rob,

Thanks a lot. I did the third way and it works. If you dont' mind.. how
come window.onload =init works and window.onload{ init(); } is not? Is
there anything speical about it? Sorry, kinda a stupid question,
obvioulsy in this case it made a differnece, i'm just curious if there
are other situations where these two will be different.

The statement:

function init() { ... }

creates a property of the window object called 'init' that is a function.

The statement - window.onload.init; - assigns a *reference* to init to
the window.onload property.

The statement - window.onload.init(); - will assign the *result* of
running init() to the window.onload property.

i.e. init() causes the function to be executed immediately (so that the
result can be assigned to the onload property).

You could also do it with an anonymous function:

window.onload = function(){ ... };

You can also add an onload event in a way that will not upset existing
onload events, follow the ideas in this thread:

<URL:http://groups.google.com/group/comp...tion+to+window.onload+&hl=en#9c228ec1551ad7ef>
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top