Alert() doesn't. Why?

D

DrKen

I'm having trouble with Alert() not working. I have created a web
page that I am only running locally in Firefox just to verify that I
have coded my JavaScript properly. I am using onsubmit in the Form
tag and I have one JS function. I put an Alert into it to be sure
that the code was doing the right thing but I don't ever see an Alert
box. I thought that Alert() was modal, so it should show up and
nothing should be doable on the web page until I've responded to the
Alert. However, when I click the submit button I get nothing.
Here is my fairly simple JavaScript:
<script type="text/javascript" src="https://www..../jquery-1.2.6.js">

function createCookie(name,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else
var expires = "";
var value = document..getElementById('USERID');
var idMessage = "The user id is: " + value;
alert(idMessage);
document.cookie = name+"="+value+expires+"; path=/";
return false;
}
</script>

Here is the Form tag:

<form name="FORM" action="" method="POST" onsubmit='return
createCookie("crmCookie", 1)'>

I know this form, before I added the JavaScript, worked fine, as it
runs in production and is used regularly. What am I missing? Thanks.

Ken
 
D

DrKen

Firefox's Error Console says that
"createCookie is not defined"

I don't know why it thinks that, so I must be missing something big.

Ken
 
T

Tim Streater

DrKen said:
I'm having trouble with Alert() not working. I have created a web
page that I am only running locally in Firefox just to verify that I
have coded my JavaScript properly. I am using onsubmit in the Form
tag and I have one JS function. I put an Alert into it to be sure
that the code was doing the right thing but I don't ever see an Alert
box. I thought that Alert() was modal, so it should show up and
nothing should be doable on the web page until I've responded to the
Alert. However, when I click the submit button I get nothing.
Here is my fairly simple JavaScript:
<script type="text/javascript" src="https://www..../jquery-1.2.6.js">

Why does this have an "src" attribute?
function createCookie(name,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else
var expires = "";
var value = document..getElementById('USERID');
var idMessage = "The user id is: " + value;
alert(idMessage);
document.cookie = name+"="+value+expires+"; path=/";
return false;
}
</script>

Here is the Form tag:

<form name="FORM" action="" method="POST" onsubmit='return
createCookie("crmCookie", 1)'>

I expect it's ignored everything between <script> and </script>
 
L

Lasse Reichstein Nielsen

DrKen said:
I'm having trouble with Alert() not working. .....
<script type="text/javascript" src="https://www..../jquery-1.2.6.js">

You can't have both src-attribute and content in the same script element.
Use two script elements for that. In this case it probably just reads the
jquery-source and ignores the content.
function createCookie(name,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));

or just:
date.setDate(date.getDate() + days);
var expires = "; expires="+date.toGMTString();
}
else
var expires = "";
var value = document..getElementById('USERID');
^ syntax error.
var idMessage = "The user id is: " + value;
alert(idMessage);
document.cookie = name+"="+value+expires+"; path=/";
return false;
}
</script>

/L
 
J

Jukka K. Korpela

<script type="text/javascript" src="https://www..../jquery-1.2.6.js">

Why does this have an "src" attribute? [...]
I expect it's ignored everything between <script> and </script>

To put it more explicitly, the script elements needs to be split in two
elements, because you cannot have both a src attribute and some content
that gets interpreted.

<script src="...">
</script>
<script>
// your own code here
</script>

The src attribute must refer to a working URL, so the one in the
original question won't do. On the other hand, this does not matter in
practice, if you don't actually make any use of the code referred to
that way.

And this won't work:
var value = document..getElementById('USERID');
(One of the periods needs to be removed. It does not matter which one. :))
 
D

DrKen

2011-06-16 20:05, Tim Streater wrote:


Why does this have an "src" attribute? [...]
I expect it's ignored everything between <script> and </script>

To put it more explicitly, the script elements needs to be split in two
elements, because you cannot have both a src attribute and some content
that gets interpreted.

<script src="...">
</script>
<script>
// your own code here
</script>

The src attribute must refer to a working URL, so the one in the
original question won't do. On the other hand, this does not matter in
practice, if you don't actually make any use of the code referred to
that way.

And this won't work:
     var value = document..getElementById('USERID');
(One of the periods needs to be removed. It does not matter which one. :))

Thanks. Actually, there is a valid URL but I didn't think that was
necessary or a good idea to list the full URL for an internal site.

Ken
 
D

DrKen

Thanks for the help. Now I see the Alert box. Unfortunately,
using this code, the document element is "null".
<script type="text/javascript">
function createCookie(name,days)
{
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));

var expires = "; expires="+date.toGMTString();
}
else
var expires = "";
var value = document.getElementById('USERID');
var idMessage = "The user id is: " + value;
alert(idMessage);
document.cookie = name+"="+value+expires+"; path=/";
return false;
}
</script>

The HTML has

<input type="text" name="USERID" />

Now that I've looked at my JavaScript book more, I see that my code,
getElementByID won't work because this input element does not have an
ID. How can I get to it that is browser-independent (I Have to
support IE, Firefox, Chrome, and Safari)? Thanks.

Ken
 
T

Thomas 'PointedEars' Lahn

That might have to do with the fact that there is no built-in Alert().
There is alert(), better written as window.alert(). ECMAScript
implementations are case-sensitive, and although some DOM implementations
are more forgiving than others, you should not rely on that.
....

You can't have both src-attribute and content in the same script element.

Yes, you can. But the content is implemented since Netscape 4.0 as a
fallback then so that it is passed on to the script engine should if the
src-specified resource cannot be retrieved. I do not know which recent
user agents also implement that fallback. It should also be noted that some
clients do not play well with markup in scripts which can happen when the
response is 404-compatible.
Use two script elements for that. In this case it probably just reads the
jquery-source and ignores the content.

Yes, very likely.
^ syntax error.

(Your marker is off in the original source as well (but to the opposite
direction, as `> >' is reduced to `>>' here). Are you not using a fixed-
width font?)


PointedEars
 
T

Thomas 'PointedEars' Lahn

Stefan said:
Actually, that's syntactically valid in Firefox, where ".." is parsed as
the E4X descendants operator. Executing the function would result in a
runtime error: "TypeError: XML descendants internal method called on
incompatible HTMLDocument".

I thought it might do that, in Firefox _4.0.x_/Gecko _2.0_ at least. Thanks
for the confirmation. (Which reminds me that the ECMAScript Support Matrix¹
should include test cases for E4X.)


PointedEars
___________
¹ <http://PointedEars.de/es-matrix>
 
J

Jukka K. Korpela

var value = document.getElementById('USERID'); [...]
The HTML has

<input type="text" name="USERID" />

Now that I've looked at my JavaScript book more, I see that my code,
getElementByID won't work because this input element does not have an
ID.

Right. So you need to add the attribute id="USERID" into the <input> tag.

There are several ways to refer to an HTML element in JavaScript, some
of them involving name="..." attributes, but you cannot arbitrarily mix
the ways. And these days it's better not know about the name="..." based
approaches, except perhaps passively (if you need to work with legacy
code that uses such approaches).

The name="..." attribute in an <input> tag is needed to name the field,
for use in name=value data to be added to the form data set. But it's
better not use it for picking up the element in JavaScript code. The
id="..." attribute, which must be unique within the document, is the way
to unambiguously identify an element.
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
(Your marker is off in the original source as well (but to the opposite
direction, as `> >' is reduced to `>>' here). Are you not using a fixed-
width font?)

I am, but the original actually contained a tab. I expect we have different
tab widths. It was supposed to point to the first '.'.

/L 'Tabs in text is a curse'
 
T

Thomas 'PointedEars' Lahn

Jukka said:
var value = document.getElementById('USERID'); [...]
The HTML has

<input type="text" name="USERID" />

"text" is the default value for this element type's `type' attribute; you
may safely omit the specification of the attribute.
Right. So you need to add the attribute id="USERID" into the <input> tag.

There are several ways to refer to an HTML element in JavaScript, some
of them involving name="..." attributes, but you cannot arbitrarily mix
the ways.

True, but per DOM Level 2 HTML, HTML5, and several implementations you can
use both IDs and names as arguments to the namedItem() methods as aliased by
the bracket property accessor in ECMAScript implementations. As posted
often before,

document.forms[…].elements[…]

and more efficient shortcuts thereof utilizing the `this' value address this
issue without the necessity for an `id' attribute.
And these days it's better not know about the name="..." based
approaches, except perhaps passively (if you need to work with legacy
code that uses such approaches).

Why? Form controls to be *submitted* need a `name' attribute (or, IOW,
*all* non-disabled form controls with a name are submitted when descendants
of a `form' element), while they do not necessarily need an `id' attribute.
Indeed one might not want to have an `id' attribute on a form control since
an ID serves a multitude of purposes (among them, global property in MSHTML,
CSS reference, and fragment identifier), not all of them which are
necessarily wanted to be addressed in a Web application.

document.forms[…].elements[…] or shortcuts thereof are by no means legacy or
proprietary ways of referencing a DOM object. While they do originate in
the so-called "DOM Level 0" (MSHTML 3/NN 3), they are standardized since a
decade in W3C DOM Level 2 HTML, and defined in HTML5 since its earliest
drafts.
The name="..." attribute in an <input> tag is needed to name the field,
for use in name=value data to be added to the form data set. But it's
better not use it for picking up the element in JavaScript code.

Why not? That too is far too general a statement.
The id="..." attribute, which must be unique within the document, is the
way to unambiguously identify an element.

But other potential disadvantages aside it is tedious to add an `id'
attribute to each and every control only to be able to refer to the DOM
object that represents it when it already has a distinct name. Indeed, it
might not even be necessary to use an `id' attribute for that purpose,
because `this' refers to the event target in event-handler attributes and
event-listeners added through standards-compliant methods.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Lasse said:
I am, but the original actually contained a tab. I expect we have
different tab widths. It was supposed to point to the first '.'.

/L 'Tabs in text is a curse'

ACK, that is why I use spaces for indentation in my source code :)


PointedEars
 
J

Jukka K. Korpela

2011-06-16 23:01 said:
"text" is the default value for this element type's `type' attribute; you
may safely omit the specification of the attribute.

Your pointless (or trolling) besserwisserism and irrelevant remarks just
confuse people. They often contain errors as well.

For example, it was completely irrelevant to the topic to say that the
attribute could be omitted. It was also incorrect, for all that you can
know, the page's style sheet may contain selectors like
input[type="text"], and the behavior may change on some browsers (on
which an element might match the selector only if the markup explicitly
sets the type attribute. Moreover, company guidelines may well say that
the type attribute be used for clarity, and then it's not safe at all to
omit it. And some code that processes the document may expect to find
that attribute. That might be poor coding style, but this implies that
your words "may safely omit" are simply not true.
 
D

DrKen

ACK, that is why I use spaces for indentation in my source code :)

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee

If I don't add an ID attribute, what is the simplest6 way to get
access to the INPUT value at submit time? Thanks.

Ken
 
T

Thomas 'PointedEars' Lahn

Jukka said:
Your pointless (or trolling) besserwisserism and irrelevant remarks just
confuse people.

You are just talking as you are able to understand. Go away.
They often contain errors as well.

Not here. And it should be noted that you, too, are not error-free (but you
won't admit that, would you?).
For example, it was completely irrelevant to the topic to say that the
attribute could be omitted.

Which is why the majority of my posting was made up of text that you did not
refer to. Go away.
It was also incorrect, for all that you can know, the page's style sheet
may contain selectors like input[type="text"], and the behavior may change
on some browsers (on which an element might match the selector only if the
markup explicitly sets the type attribute.

Utter nonsense. If you knew what you are talking about, you would know that
applications are not required to honor such a selector in the first place.
It is therefore error-prone to begin with to rely on it. (We have been over
this; I specifically pointed out in that discussion that there is a
difference between versions of MSHTML, and between MSHTML and Gecko, for
example, that is backed by the Specification.)
[red herring]

*GO* *AWAY!*


PointedEars
 
T

Thomas 'PointedEars' Lahn

Gregor said:
Am 2011-06-16 23:46, DrKen meinte:
If I don't add an ID attribute, what is the simplest6 way to get
access to the INPUT value at submit time? Thanks.

Depends. Name or index are options. [1]

[…]
[1] https://developer.mozilla.org/en/DOM/form.elements

| var inputs = document.getElementById("form1").elements;
| var inputByIndex = inputs[2];
| var inputByName = inputs["login"];

It is a strange example, is it not? They could have easily written

var inputs = document.forms["form1"].elements;
var inputByIndex = inputs[2];
var inputByName = inputs["login"];

instead.

But it is necessary to point out (which I would have directly had not a
Googlodyte replied) that something *along*

<form … onsubmit="… this.elements['…'] …">

would suffice. Unfortunately, one does not see the more efficient shortcut
reference in examples very often as they are too often oversimplified, not
recognizing the use-case.

BUT: These days referring to form controls with the bracket property
accessor is only *simpler* if the names are unique (otherwise you have to
deal with the collections). However, a contextual property access should be
*faster* than a globally operating function call.


BTW, you should fix your Message-ID or tell your service provider to do so.


PointedEars
 
D

Denis McMahon

Firefox's Error Console says that
"createCookie is not defined"

I don't know why it thinks that, so I must be missing something big.

What does the error console say when you load the page?

Rgds

Denis McMahon
 
D

Denis McMahon

If I don't add an ID attribute, what is the simplest6 way to get access
to the INPUT value at submit time? Thanks.

Adding an id attribute _is_ the simplest way:

<input type='type' name='name' id='id'>

element_value = getElementById('id').value;

and is pretty much universal. Note that I've used html 4 markup. You may
need to change ">" to " />"

You can, instead, I think, use:

document.forms['form'].['input_name'].value

or something similar, but (a) it's so long I did that I'd have to look it
up to make sure I was right, and (b) I'm sure that at least two users of
the newsgroup would then get into a long and involved debate over which
of two or more possible methods of referencing the element by name was
actually best, right, or proper.

Rgds

Denis McMahon
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top