Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Alert() doesn't. Why?

Reply
Thread Tools

Alert() doesn't. Why?

 
 
DrKen
Guest
Posts: n/a
 
      06-16-2011
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
 
Reply With Quote
 
 
 
 
DrKen
Guest
Posts: n/a
 
      06-16-2011
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
 
Reply With Quote
 
 
 
 
Tim Streater
Guest
Posts: n/a
 
      06-16-2011
In article
<(E-Mail Removed)>,
DrKen <(E-Mail Removed)> wrote:

> 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>

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      06-16-2011
DrKen <(E-Mail Removed)> writes:

> 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
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      06-16-2011
2011-06-16 20:05, Tim Streater wrote:

>> <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. )

--
Yucca, http://www.cs.tut.fi/~jkorpela/
 
Reply With Quote
 
DrKen
Guest
Posts: n/a
 
      06-16-2011
On Jun 16, 10:48*am, "Jukka K. Korpela" <(E-Mail Removed)> wrote:
> 2011-06-16 20:05, Tim Streater wrote:
>
>
>
> >> <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. )
>
> --
> Yucca,http://www.cs.tut.fi/~jkorpela/


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
 
Reply With Quote
 
DrKen
Guest
Posts: n/a
 
      06-16-2011
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-16-2011
Lasse Reichstein Nielsen wrote:

> DrKen <(E-Mail Removed)> writes:
>> I'm having trouble with Alert() not working.


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.

> ....
>> <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.


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.

>> var expires = "; expires="+date.toGMTString();
>> }
>> else
>> var expires = "";
>> var value = document..getElementById('USERID');

> ^ 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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-16-2011
Stefan Weiss wrote:

> On 16/06/11 19:37, Lasse Reichstein Nielsen wrote:
>> DrKen <(E-Mail Removed)> writes:
>>> var value = document..getElementById('USERID');

>> ^ syntax error.

>
> 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>
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
Jukka K. Korpela
Guest
Posts: n/a
 
      06-16-2011
2011-06-16 22:06, DrKen wrote:

> 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.

--
Yucca, http://www.cs.tut.fi/~jkorpela/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments