joemac wrote:
> I'm just starting out with javascript and the following is not working
> as it should. The expected prompt dialog box never appears.
>
> <head>
> <script language="JavaScript"><!--
> var name;
> name=prompt("Please enter your name.","");
> document.write("My name is: ", name);
> // --></script>
> </head>
> <body>
> <p>Did it work out ok?
> </body>
> </html>
>
> I've tried replacing
> <script language="JavaScript">
> with
> <script type="text/javascript">
> but this makes no difference in the results, which are as follows:
>
> My name is:
>
> Did it work out ok?
>
> What am I doing wrong?
I donno... It works as it posted, maybe your did not type the name in?
Few advises though: document.write() clears the document content after
the page has been finished to load. It is almost never what you want.
You may want to use DOM methods instead. And DOM methods are not
realibly available until the page finished to load (neither your
functions). Thus the preffered sequence would be:
1) You load the page first
2) The "load" event calls your function
3) Your function does all imaginable (and unimaginable ;-) things with
your page
<html>
<head>
<title>Hello world!</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
// Global variable:
var myName = "";
function myFunction() {
// Local variable:
var sysOut = document.getElementById("sysOut");
myName = prompt("Please enter your name:","");
if (myName) {
sysOut.innerHTML = "Your name is: " + myName;
}
else {
sysOut.innerHTML = "You did not tell me your name... :-(";
}
}
</script>
</head>
<body onload="myFunction()">
<div id="sysOut">Did it work out ok?</div>
</body>
</html>
Received on Sat Dec 3 04:33:42 2005