Weird Javascript "test is not defined" error.

G

Gregor Kofler

Sytse meinte:
See code below.
Why does my browser complain that test is not defined, while the alert
call is valid?

Because your document gets overwritten after being loaded, and therefore
test() no longer exists.

Gregor
 
S

Sytse

See code below.
Why does my browser complain that test is not defined, while the alert
call is valid?
It does not matter in which order I define create and test.
It seems to be a scope issue, but how can this be solved?

Regards,

Sytse.

<HTML>
<HEAD>
<script language="JavaScript" type="text/javascript">
function test() {
alert ("test");
}

function create() {
document.write ("<A href=\"#\" onClick=\"test();\">test</A>
<BR />");
document.write ("<A href=\"#\"
onClick=\"alert('alert');\">alert</A>");
}
</script>
</HEAD>
<BODY onLoad="create()">
</BODY>
</HTML>
 
S

Stevo

Sytse said:
See code below.
Why does my browser complain that test is not defined, while the alert
call is valid?
It does not matter in which order I define create and test.
It seems to be a scope issue, but how can this be solved?

function create() {
document.write ("<A href=\"#\" onClick=\"test();\">test</A>
<BR />");
document.write ("<A href=\"#\"
onClick=\"alert('alert');\">alert</A>");
<BODY onLoad="create()">

You can't use document.write *after* the onLoad event has been
triggered. The document.write call is used to add stuff to a page that's
currently being built. The onload event is triggered after it's finished
building (it's already built). So making a call to document.write after
that, will make the browser start a brand new page with that new HTML
that you're document.writing. If you were to view-source, you'd see only
the second of the first of those two document.write HTML strings
present. It would never have reached the second one because the first
one started a new page.
 
G

Gregor Kofler

Sytse meinte:

Please stop top-posting. Thanks.
OK thanks.
Is there a trick to overcome this issue?

Easy: Don't use document.write() after loading a page.

Why do you write at all? Apend anchor elements to the body in your
create()-function.

Something like

var a = document.createElement("a");
a.appendChild(document.createTextNode("test");
a.href = "...";
a.onclick = test;
document.body.appendChild(a);

Gregor
 
S

Sytse

OK thanks.
Easy: Don't use document.write() after loading a page.

Why do you write at all? Apend anchor elements to the body in your
create()-function.

Something like

var a = document.createElement("a");
a.appendChild(document.createTextNode("test");
a.href = "...";
a.onclick = test;
document.body.appendChild(a);

Gregor


Great! Thanks.

Guess I'll have to dig into javascript a bit more.

Regards,

Sytse.
 

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

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top