By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,462 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,462 IT Pros & Developers. It's quick & easy.

prompt not working

P: n/a
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?

Joe

Nov 30 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
VK

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>

Nov 30 '05 #2

P: n/a
>I donno... It works as it posted, maybe your did not type the name in?

As I said, the dialog box does not appear - so there is no place for me
to type in the name.
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.


Thanks for the tip but at this point I am simply following the
instructions as given in the book that I am using to learn the
language. If it is not the best approach then I'm sure the author will
explain that later on and give new examples. Since you said the code
worked for you as posted then it must be viable. There must be
something different about my runtime environment which causes it not to
work for me, and this is what I need to get to the bottom of.

I tried executing the code that you provided as a counter example and
it likewise did not work for me as it should have. No prompt box
appeared and I got the following result:

You did not tell me your name... :-(

Joe

Nov 30 '05 #3

P: n/a
VK
joemac wrote:
I tried executing the code that you provided as a counter example and
it likewise did not work for me as it should have. No prompt box
appeared and I got the following result:

You did not tell me your name... :-(


It means then that

Option A) You browser doesn't support JavaScript - highly unlikely
but...

Option B) You raised security settings too high so JavaScript became
disabled on your browser.

In the latter case (if Internet Explorer):
1) run Internet Explorer
2) Tools > Internet Options... > Security tab
3) Select "Internet" zone and click "Custom level"
4) Find the "Active scripting" section and set it to "Enable"
The rest of settings you may leave is they are.

If you don't want (or not allowed) to change any settings, then you
have to go to http://www.mozilla.org, get the newest version of Firefox
browser and use it to learn JavaScript. And use Internet Explorer to
browse the Internet.

Nov 30 '05 #4

P: n/a
VK wrote:
joemac wrote:
I tried executing the code that you provided as a counter example and
it likewise did not work for me as it should have. No prompt box
appeared and I got the following result:

You did not tell me your name... :-(
It means then that

Option A) You browser doesn't support JavaScript - highly unlikely
but...


I'm using IE version 6. Seems unlikely to me as well.
Option B) You raised security settings too high so JavaScript became
disabled on your browser.

In the latter case (if Internet Explorer):
1) run Internet Explorer
2) Tools > Internet Options... > Security tab
3) Select "Internet" zone and click "Custom level"
4) Find the "Active scripting" section and set it to "Enable"
The rest of settings you may leave is they are.


I thought maybe we were onto something here but it turns out that my
Active Scripting was already Enabled. I checked my "Intranet" settings
as well and it is enabled there also.

Any other ideas?

Thanks, Joe

Nov 30 '05 #5

P: n/a
VK

joemac wrote:
Any other ideas?


Where is no more, sorry...

My variant of script is working for sure.
IE 6 has default support for JScript for sure.
You are saying that JScript is enabled.

But it ignores the script - it means that something of three statements
above is wrong.

I just checked the script again - it works. It is so primitive - there
is no space for an error. Plus someone would point it already out.

IE 6 indeed supports JScript: this common knowledge fact can be easily
checked at microsoft.com.

So: JScript is disabled on your browser. I have no idea how is it
possible. Maybe you have some overly-agressive 3rd party anti-virus
which strips out any script from the page? The problem seems not
reparable remotely.

For the time being go to <http://www.mozilla.com> and download Firefox
1.5 (it's only 5Mb). Install it and open the test page in it to see if
it works. This is the only way to know if crazy things are going with
your IE browser or with your Windows.

Nov 30 '05 #6

P: n/a
Well my test page works fine under FireFox so I guess the problem must
lie somewhere within IE. If anyone has any ideas what the problem
could be, please post. Thanks.

Joe

VK wrote:
joemac wrote:
Any other ideas?


Where is no more, sorry...

My variant of script is working for sure.
IE 6 has default support for JScript for sure.
You are saying that JScript is enabled.

But it ignores the script - it means that something of three statements
above is wrong.

I just checked the script again - it works. It is so primitive - there
is no space for an error. Plus someone would point it already out.

IE 6 indeed supports JScript: this common knowledge fact can be easily
checked at microsoft.com.

So: JScript is disabled on your browser. I have no idea how is it
possible. Maybe you have some overly-agressive 3rd party anti-virus
which strips out any script from the page? The problem seems not
reparable remotely.

For the time being go to <http://www.mozilla.com> and download Firefox
1.5 (it's only 5Mb). Install it and open the test page in it to see if
it works. This is the only way to know if crazy things are going with
your IE browser or with your Windows.


Nov 30 '05 #7

P: n/a
joemac wrote:
Well my test page works fine under FireFox so I guess the problem must
lie somewhere within IE. If anyone has any ideas what the problem
could be, please post. Thanks.


Please don't top post, reply below quoted text and trim unnecessary
material.

I'd suggest moving the document.write (actually the whole script)out of
the head element. MS have given the 'prompt' method an optional default
text parameter that Firefox seems to ignore.

In IE 6 if the default text isn't specified, 'undefined' is put into the
prompt input field, which unsettles some users. Try the following:
<html>
<head><title>Write test</title></head>
<body>
<script type="text/javascript">
var aName = prompt('Enter your name please','');
document.write(aName);
</script>
</body>
</html>
If that doesn't work, Google 'mark of the web'.

[...]

--
Rob
Dec 1 '05 #8

P: n/a
Lee wrote:

Since you know that you should use "text/javascript", why did you
post code with the obsolete form, instead?
I *don't* know which form of usage is correct. According to the text
that I'm using ("Introduction to Interactive Programming on the
Internet using HTML & JavaScript", by Craig D. Knuckles), "<script
language="JavaScript">" is the correct form of usage. The only reason
I got the idea of trying "<script type="text/javascript>" is because I
took a look at some sample code on a web site geared towards Javascript
programmers.
Don't use "name" as a variable. The window already has an attribute
called "name".

Try this:

<html>
<head>
<script type="text/javascript>
var myName;
myName=prompt("Please enter your name.","");
document.write("My name is: ", myName);
</script>
</head>
<body>
<p>Did it work out ok?</p>
</body>
</html>


Ok, I tried your example above and it doesn't work for me either.
There must be something wrong with my IE configuration but I have no
idea what.

Joe

Dec 1 '05 #9

P: n/a
RobG wrote:
joemac wrote:
Well my test page works fine under FireFox so I guess the problem must
lie somewhere within IE. If anyone has any ideas what the problem
could be, please post. Thanks.
Please don't top post, reply below quoted text and trim unnecessary
material.


Ok.
I'd suggest moving the document.write (actually the whole script)out of
the head element. MS have given the 'prompt' method an optional default
text parameter that Firefox seems to ignore.

In IE 6 if the default text isn't specified, 'undefined' is put into the
prompt input field, which unsettles some users. Try the following:
<html>
<head><title>Write test</title></head>
<body>
<script type="text/javascript">
var aName = prompt('Enter your name please','');
document.write(aName);
</script>
</body>
</html>
Tried it. Doesn't work. I noticed that you used single rather than
double quotes for the prompt parameters. Was that intentional or a
typo? If single quotes are the correct way of specifying strings in
JavaScript then perhaps the text I'm learning from is too rife with
mistakes to be useful.
If that doesn't work, Google 'mark of the web'. From what I just read about it it seems to only be an issue in Windows

XP SP 2. I am running Windows 2000 Professional SP 4. Let me know if
I'm wrong ... if it is in fact also an issue for Win2k. Thanks.

Joe

Dec 1 '05 #10

P: n/a
joemac wrote:
Lee wrote:
Since you know that you should use "text/javascript", why did you
post code with the obsolete form, instead?
I *don't* know which form of usage is correct.


Bad enough.
According to the text that I'm using ("Introduction to Interactive
Programming on the Internet using HTML & JavaScript", by Craig D.
Knuckles), "<script language="JavaScript">" is the correct form of
usage.
Burn it. Obviously its content is either completely wrong or badly
outdated. (The HTML 4.01 Specification is dated December 24, _1999_).
The only reason I got the idea of trying "<script type="text/javascript>"
is because I took a look at some sample code on a web site geared towards
Javascript programmers.


<URL:http://validator.w3.org/> pretty much tells you what is correct
regarding HTML and what is not.

<URL:http://www.pointedears.de/scripts/mime-types/> (which I had no
time to complete with your responses yet, sorry) tells you what is
current, and pretty much tells you what is the prudent MIME type and
what is not.
PointedEars
Dec 1 '05 #11

P: n/a
joemac wrote:
RobG wrote:

[...]
<html>
<head><title>Write test</title></head>
<body>
<script type="text/javascript">
var aName = prompt('Enter your name please','');
document.write(aName);
</script>
</body>
</html>

Tried it. Doesn't work. I noticed that you used single rather than
double quotes for the prompt parameters. Was that intentional or a
typo? If single quotes are the correct way of specifying strings in
JavaScript then perhaps the text I'm learning from is too rife with
mistakes to be useful.


Intentional. I like to use double quotes in HTML and single in script,
it makes it the target environment more obvious.

If that doesn't work, Google 'mark of the web'.


From what I just read about it it seems to only be an issue in Windows

XP SP 2. I am running Windows 2000 Professional SP 4. Let me know if
I'm wrong ... if it is in fact also an issue for Win2k. Thanks.


Just fishing! I get the same behaviour, Thomas does not. He is using
Firefox on Linux, we are on Windows.

This seems to be already reported as a bug:

<URL: https://bugzilla.mozilla.org/show_bug.cgi?id=312466 >

--
Rob
Dec 1 '05 #12

P: n/a
Thomas 'PointedEars' Lahn wrote:

<URL:http://validator.w3.org/> pretty much tells you what is correct
regarding HTML and what is not.


I ran my code through the above validator. First time it complained
about the missing DOCTYPE, so I added one. Second time it strongly
recommended a Character Encoding specification, so I added one. On the
third pass it gave it's seal of approval that the code is valid.
Delightful! Now if someone could explain why it still doesn't work I'd
be ecstatic. Here is the modified version...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;
charset=ISO-8859-1">
<TITLE>Java Script Test</TITLE>
<SCRIPT TYPE="text/javascript">
var myName;
myName=prompt("Please enter your name.","");
document.write("My name is: ", myName);
</SCRIPT>
</HEAD>
<BODY>
<p>Did it work out ok?
</BODY>
</HTML>

Joe

Dec 1 '05 #13

P: n/a
joemac wrote:
Thomas 'PointedEars' Lahn wrote:
<URL:http://validator.w3.org/> pretty much tells you what is correct
regarding HTML and what is not.
I ran my code through the above validator. First time it complained
about the missing DOCTYPE, so I added one. Second time it strongly
recommended a Character Encoding specification, so I added one.


Be sure you also serve it with the correct Content-Type HTTP header.
<URL:http://web-sniffer.net/>
[...] Now if someone could explain why it still doesn't work I'd
be ecstatic. Here is the modified version...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
So far you set browsers with DOCTYPE sniffing to use Compatibility/Quirks
Mode.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

will trigger Standards Compliance Mode of browsers. The W3C Markup
Validator does not tell you this because it uses an SGML parser that
uses an catalog file and looks up the system identifier (the URI of
the DTD) there, matching it against the public identifier (that you
already included above.)

<http://www.w3.org/TR/html4/struct/global.html#h-7.2>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;
charset=ISO-8859-1">
<TITLE>Java Script Test</TITLE>
<SCRIPT TYPE="text/javascript">
var myName;
myName=prompt("Please enter your name.","");
That is the same as

var myName = prompt("Please enter your name.", "");
document.write("My name is: ", myName);
Try

document.write("My name is: " + myName);

instead.
</SCRIPT>
</HEAD>
<BODY>
<p>Did it work out ok?
Even if not required in HTML 4.01 Transitional, you should
include the close tag </p>.
</BODY>
</HTML>


(Even though HTML is case-insensitive in that regard, it
is a Good Thing to lowercase all tag and attribute names.)

You have not moved the `script' element into the `body'
element as was suggested before. Try that first.
HTH

PointedEars
Dec 1 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.