Alert() doesn't. Why?

Discussion in 'Javascript' started by DrKen, Jun 16, 2011.

  1. DrKen

    DrKen Guest

    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
    DrKen, Jun 16, 2011
    #1
    1. Advertising

  2. DrKen

    DrKen Guest

    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
    DrKen, Jun 16, 2011
    #2
    1. Advertising

  3. DrKen

    Tim Streater Guest

    In article
    <>,
    DrKen <> 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
    Tim Streater, Jun 16, 2011
    #3
  4. DrKen <> 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'
    Lasse Reichstein Nielsen, Jun 16, 2011
    #4
  5. 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/
    Jukka K. Korpela, Jun 16, 2011
    #5
  6. DrKen

    DrKen Guest

    On Jun 16, 10:48 am, "Jukka K. Korpela" <> 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
    DrKen, Jun 16, 2011
    #6
  7. DrKen

    DrKen Guest

    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
    DrKen, Jun 16, 2011
    #7
  8. Lasse Reichstein Nielsen wrote:

    > DrKen <> 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.)
    Thomas 'PointedEars' Lahn, Jun 16, 2011
    #8
  9. Stefan Weiss wrote:

    > On 16/06/11 19:37, Lasse Reichstein Nielsen wrote:
    >> DrKen <> 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$>
    Thomas 'PointedEars' Lahn, Jun 16, 2011
    #9
  10. 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/
    Jukka K. Korpela, Jun 16, 2011
    #10
  11. Thomas 'PointedEars' Lahn <> writes:

    > Lasse Reichstein Nielsen wrote:
    >
    >> DrKen <> writes:


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


    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'
    --
    Lasse Reichstein Holst Nielsen
    'Javascript frameworks is a disruptive technology'
    Lasse Reichstein Nielsen, Jun 16, 2011
    #11
  12. Jukka K. Korpela wrote:

    > 2011-06-16 22:06, DrKen wrote:
    >> 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.

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


    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
    --
    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.)
    Thomas 'PointedEars' Lahn, Jun 16, 2011
    #12
  13. Lasse Reichstein Nielsen wrote:

    > Thomas 'PointedEars' Lahn <> writes:
    >> Lasse Reichstein Nielsen wrote:
    >>> DrKen <> writes:
    >>>> 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?)

    >
    > 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
    --
    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
    Thomas 'PointedEars' Lahn, Jun 16, 2011
    #13
  14. 2011-06-16 23:01, Thomas 'PointedEars' Lahn wrote:

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


    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.

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
    Jukka K. Korpela, Jun 16, 2011
    #14
  15. DrKen

    DrKen Guest

    On Jun 16, 1:04 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > Lasse Reichstein Nielsen wrote:
    > > Thomas 'PointedEars' Lahn <> writes:
    > >> Lasse Reichstein Nielsen wrote:
    > >>> DrKen <> writes:
    > >>>> 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?)

    >
    > > 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
    > --
    > 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
    DrKen, Jun 16, 2011
    #15
  16. Jukka K. Korpela wrote:

    > 2011-06-16 23:01, Thomas 'PointedEars' Lahn wrote:
    >>>> <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.

    >
    > 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
    --
    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.)
    Thomas 'PointedEars' Lahn, Jun 17, 2011
    #16
  17. Am 2011-06-16 23:46, DrKen meinte:
    > On Jun 16, 1:04 pm, Thomas 'PointedEars' Lahn <>
    > wrote:
    >> Lasse Reichstein Nielsen wrote:
    >>> Thomas 'PointedEars' Lahn <> writes:
    >>>> Lasse Reichstein Nielsen wrote:
    >>>>> DrKen <> writes:
    >>>>>> 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?)

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


    Depends. Name or index are options. [1]

    Gregor

    [1] https://developer.mozilla.org/en/DOM/form.elements
    Gregor Kofler, Jun 17, 2011
    #17
  18. Gregor Kofler wrote:

    > 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
    --
    Danny Goodman's books are out of date and teach practices that are
    positively harmful for cross-browser scripting.
    -- Richard Cornford, cljs, <cife6q$253$1$> (2004)
    Thomas 'PointedEars' Lahn, Jun 17, 2011
    #18
  19. On Thu, 16 Jun 2011 09:26:47 -0700, DrKen wrote:

    > 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
    Denis McMahon, Jun 17, 2011
    #19
  20. On Thu, 16 Jun 2011 14:46:57 -0700, DrKen wrote:

    > 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
    Denis McMahon, Jun 17, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bouke Woudstra

    (noob alert) why doesn't this work?

    Bouke Woudstra, Mar 22, 2005, in forum: Python
    Replies:
    1
    Views:
    251
    Diez B. Roggisch
    Mar 22, 2005
  2. Bouke Woudstra

    Re: (noob alert) why doesn't this work?

    Bouke Woudstra, Mar 22, 2005, in forum: Python
    Replies:
    2
    Views:
    265
    Bengt Richter
    Mar 23, 2005
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,842
    Smokey Grindel
    Dec 2, 2006
  4. Mersh
    Replies:
    0
    Views:
    487
    Mersh
    Mar 13, 2007
  5. Ganesh
    Replies:
    0
    Views:
    551
    Ganesh
    Jun 29, 2007
Loading...

Share This Page