473,386 Members | 1,830 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,386 software developers and data experts.

undefined data in javascript

I have no idea why id is undefined in the arrow below. Please advise. Thanks!!

<html>
<head>
<script type="text/javascript">
var id;
function window_onload()
{
id = "8888";
alert("ID = " + id);
}
</script>
</head>
<H2>Page 2</H2>
<body onload="window_onload()">
<P>id = <script type="text/javascript">document.write(id);</script> //<****
</body>
</html>
Jul 20 '05 #1
4 2267
Matt wrote:
I have no idea why id is undefined in the arrow below. Please advise. Thanks!!

<html>
<head>
<script type="text/javascript">
var id;
function window_onload()
{
id = "8888";
alert("ID = " + id);
}
</script>
</head>
<H2>Page 2</H2>
<body onload="window_onload()">
<P>id = <script type="text/javascript">document.write(id);</script> //<****
</body>
</html>

Because its value is not set until the onload event fires, which is
*after* you try to write it, so its not set yet. So, you get undefined
(which it is).

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 20 '05 #2

Randy Webb wrote:
Matt wrote:
I have no idea why id is undefined in the arrow below. Please advise.
Thanks!!

<html>
<head>
<script type="text/javascript">
var id;
function window_onload()
{
id = "8888";
alert("ID = " + id);
}
</script>
</head>
<H2>Page 2</H2>
<body onload="window_onload()">
<P>id = <script type="text/javascript">document.write(id);</script>
//<****
</body>
</html>


Because its value is not set until the onload event fires, which is
*after* you try to write it, so its not set yet. So, you get undefined
(which it is).


Yup, I tried this:

<html>
<head>
<script type="text/javascript">
var id;
function window_onload() {
//id = "8888";
alert("ID = " + id);
}
</script>
</head>

<body onload="window_onload()">
<script type="text/javascript">
var id="8888";
</script>
<form>
<P>id = <script type="text/javascript">document.write(id);</script>
</form>
</body>
</html>

Jul 20 '05 #3
Lee
Matt said:

I have no idea why id is undefined in the arrow below. Please advise. Thanks!!

<html>
<head>
<script type="text/javascript">
var id;
function window_onload()
{
id = "8888";
alert("ID = " + id);
}
</script>
</head>
<H2>Page 2</H2>
<body onload="window_onload()">
<P>id = <script type="text/javascript">document.write(id);</script> //<****
</body>
</html>


id isn't defined until the onLoad event handler executes,
but the inline script tries to write its value as the page is still loading.

Jul 20 '05 #4
oops... forgot to clean it up first

<html>
<head>
<script type="text/javascript">
var id;
function window_onload() {
alert("ID = " + id);
}
</script>
</head>

<body onload="window_onload()">
<script type="text/javascript">
id="8888";
</script>
<P>id = <script type="text/javascript">document.write(id);</script>
</body>
</html>

Jul 20 '05 #5

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

Similar topics

4
by: Mike | last post by:
I am having a problem when a field is spaces being undefined. I wasn't sure if the problem was Excel or Javascript, so I thought I would post here first. The users are able to select from a drop...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
13
by: Oleg Konovalov | last post by:
Hi, I am working on a web application which among other things uses DHTML, Java and Javascript. It populates web page based on the contents of the database (resultset), and next to each row...
13
by: -Lost | last post by:
You can test for an undefined value in two ways: function blah(x) { if (x == undefined) { x = 'default value'; } } That could also have been written: function blah(x)
2
by: thuythu | last post by:
Please help me.... I used and Javascript to view the data. But when i click button open a popup windows, then select data and click save button. The popup close and return the main page, but the...
3
by: blackrunner | last post by:
ERROR in my Query?! ERROR: Element GESCHLECHT is undefined in FORM. i think everything ok. Maby somebody can help me here Element GESCHLECHT is undefined in FORM. The error occurred...
4
hemantbasva
by: hemantbasva | last post by:
We have designed an aspx page having five ajax tab in it. the data of first four are designed on page whereas for the fifth tab it renders a user control named MYDOMAIN. in the tab container's even...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.