prompt not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joemac

    prompt not working

    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="JavaS cript"><!--
    var name;
    name=prompt("Pl ease 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="JavaS cript">
    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

  • Thomas 'PointedEars' Lahn

    #2
    Re: prompt not working

    joemac wrote:
    [color=blue]
    > 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="JavaS cript"><!--
    > var name;
    > name=prompt("Pl ease 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="JavaS cript">
    > 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?[/color]

    A) a) You are declaring an XHTML document type and serve it
    correctly as application/xhtml+xml. Not at all wrong,
    but it would explain points Ab) and B) below.

    b) document.write( ) does not work in XHTML 1.1 and later.
    It also does not work in XHTML 1.0 yet, even though W3C
    DOM Level 2 HTML says it should.

    B) You successfully commented out script content with the
    empty declaration `<!-- ... -->'.

    C) 1. There is no global prompt() method in your UA, meaning
    that either the Global Object is not the same object the
    `window' reference points to or this host environment
    method is not supported by your UA's Application Object
    Model (AOM).

    2. Therefore, a ReferenceError occurs and further expressions
    are not evaluated.

    D) Script support is not present, i.e. either it is
    disabled or not available in the first place.

    <URL:http://jibbering.com/faq/#FAQ4_43>


    PointedEars

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: prompt not working

      Thomas 'PointedEars' Lahn wrote:
      [color=blue]
      > joemac wrote:[color=green]
      >> 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="JavaS cript"><!--
      >> var name;
      >> name=prompt("Pl ease enter your name.","");
      >> document.write( "My name is: ", name);
      >> // --></script>
      >> </head>
      >> <body>
      >> <p>Did it work out ok?
      >> </body>
      >> </html>
      >>
      >> [...]
      >> My name is:
      >>
      >> Did it work out ok?
      >>
      >> What am I doing wrong?[/color][/color]

      Re-reviewing this:
      [color=blue]
      > [...]
      > A) a) You are declaring an XHTML document type and serve it
      > correctly as application/xhtml+xml. Not at all wrong,
      > but it would explain points Ab) and B) below.
      >
      > b) document.write( ) does not work in XHTML 1.1 and later.
      > It also does not work in XHTML 1.0 yet, even though
      > W3C DOM Level 2 HTML says it should.[/color]

      That does not apply here, document.write( ) does something, which
      I overlooked at first.
      [color=blue]
      > B) You successfully commented out script content with the
      > empty declaration `<!-- ... -->'.[/color]

      Since "My name is: " is displayed, this would not apply here either.

      However, commenting out script contents like this can have such
      harmful effects and since it is not needed either, you should not
      do it anyway.
      [color=blue]
      > C) 1. There is no global prompt() method in your UA, meaning
      > that either the Global Object is not the same object the
      > `window' reference points to or this host environment
      > method is not supported by your UA's Application Object
      > Model (AOM).[/color]

      That is still a possibility. Since document.write( ..., name) was
      used and not document.write( ... + name), a DOM implementation may
      either decide not to display `undefined' or not to support
      additional arguments for that method.
      [color=blue]
      > 2. Therefore, a ReferenceError occurs and further expressions
      > are not evaluated.[/color]

      That does not appear to happen, since my "My name is:" is displayed.
      [color=blue]
      > D) Script support is not present, i.e. either it is
      > disabled or not available in the first place.[/color]

      That's not very likely either, since "My name is:" is created via
      client-side script.

      It appears that C) is the only possibility that remains.

      Sorry for causing confusion :-/


      PointedEars

      Comment

      • joemac

        #4
        Re: prompt not working

        Thomas 'PointedEars' Lahn wrote:[color=blue]
        >
        > A) a) You are declaring an XHTML document type and serve it
        > correctly as application/xhtml+xml. Not at all wrong,
        > but it would explain points Ab) and B) below.[/color]

        Since there are no explicit declarations about document types in my
        code I infer from the above that XHTML must be the "default" document
        type. Is that correct?
        [color=blue]
        > b) document.write( ) does not work in XHTML 1.1 and later.
        > It also does not work in XHTML 1.0 yet, even though W3C
        > DOM Level 2 HTML says it should.
        >
        > B) You successfully commented out script content with the
        > empty declaration `<!-- ... -->'.[/color]

        The book that I am using says to use comment tags to enclose all script
        elements so as to prevent wrongful interpretation by browsers lacking
        the ability to process said javascript elements. Are you saying that
        there are some browsers that are capable of processing javascript but
        do know enough to overlook comment tags inside of script begin and end
        tags?
        [color=blue]
        > C) 1. There is no global prompt() method in your UA, meaning
        > that either the Global Object is not the same object the
        > `window' reference points to or this host environment
        > method is not supported by your UA's Application Object
        > Model (AOM).[/color]

        What is "UA"?
        [color=blue]
        > 2. Therefore, a ReferenceError occurs and further expressions
        > are not evaluated.[/color]

        Is it possible to determine that this (ReferenceError ) has occurred,
        and why?
        [color=blue]
        > D) Script support is not present, i.e. either it is
        > disabled or not available in the first place.[/color]

        How would I go about determining this and correcting it if necessary?
        [color=blue]
        > <URL:http://jibbering.com/faq/#FAQ4_43>[/color]

        I examined the referenced article. I do not see any "little yellow
        triangle". In fact I see no triangle at all, of any color.

        Joe

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: prompt not working

          joemac wrote:
          [color=blue]
          > Thomas 'PointedEars' Lahn wrote:[color=green]
          >> A) a) You are declaring an XHTML document type and serve it
          >> correctly as application/xhtml+xml. Not at all wrong,
          >> but it would explain points Ab) and B) below.[/color]
          >
          > Since there are no explicit declarations about document types in my
          > code I infer from the above that XHTML must be the "default" document
          > type. Is that correct?[/color]

          No, it is not. The document type must be explicitly declared, you
          should declare one of the HTML 4.01 document types. See the Spec.
          [color=blue][color=green]
          >> b) document.write( ) does not work in XHTML 1.1 and later.
          >> It also does not work in XHTML 1.0 yet, even though W3C
          >> DOM Level 2 HTML says it should.
          >>
          >> B) You successfully commented out script content with the
          >> empty declaration `<!-- ... -->'.[/color]
          >
          > The book that I am using says to use comment tags to enclose all script
          > elements so as to prevent wrongful interpretation by browsers lacking
          > the ability to process said javascript elements.[/color]

          Your book (which?) is outdated.

          There are no such browsers left that would still conform to Internet
          standards. See RFC2854 and, of course, previous discussions on the
          subject.
          [color=blue]
          > Are you saying that there are some browsers that are capable of
          > processing javascript but do know enough to overlook comment tags
          > inside of script begin and end tags?[/color]

          Probably. The HTML specification does not state it as a MUST or
          SHOULD, and examples therein are not normative. In fact, in HTML
          the `script' element's content is CDATA that is _not_ really
          parsed. (The only parsing that MUST take place is looking for
          the CDATA-ending ETAGO delimiter `</'.)

          Furthermore, there is no reference material that states script
          engines MUST ignore `<!--' at the beginning of script code. And
          in XHTML, where an XML parser is used and the `script' element's
          content is PCDATA by default, it is allowed to ignore commented
          content by removing comments from source before building the
          parse tree. (After all, they are only _comments_.)
          [color=blue][color=green]
          >> C) 1. There is no global prompt() method in your UA, meaning
          >> that either the Global Object is not the same object the
          >> `window' reference points to or this host environment
          >> method is not supported by your UA's Application Object
          >> Model (AOM).[/color]
          >
          > What is "UA"?[/color]

          ([X]HTML) User Agent. Web browsers are the greatest subset.
          [color=blue][color=green]
          >> 2. Therefore, a ReferenceError occurs and further expressions
          >> are not evaluated.[/color]
          >
          > Is it possible to determine that this (ReferenceError ) has occurred,[/color]

          Yes, if the error reporting feature of your UA does not suffice,
          you could use

          try
          {
          // statements that may cause errors
          }
          catch (e)
          {
          alert(e.name + ": " + e.message);
          }

          provided it is supported (ECMAScript 3 compliant implementation) .
          Or use proprietary

          onerror = function()
          {
          alert("Error!") ;
          onerror = null;
          return true;
          }
          [color=blue]
          > and why?[/color]

          Only line-wise debugging can show this.
          [color=blue][color=green]
          >> D) Script support is not present, i.e. either it is
          >> disabled or not available in the first place.[/color]
          >
          > How would I go about determining this and correcting it if necessary?[/color]

          Depends on the user agent.
          [color=blue][color=green]
          >> <URL:http://jibbering.com/faq/#FAQ4_43>[/color]
          >
          > I examined the referenced article. I do not see any "little yellow
          > triangle". In fact I see no triangle at all, of any color.[/color]

          Which user agent(s) have you tested with?


          PointedEars

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: prompt not working

            Thomas 'PointedEars' Lahn wrote:
            [color=blue]
            > joemac wrote:[color=green]
            >> Thomas 'PointedEars' Lahn wrote:[color=darkred]
            >>> 2. Therefore, a ReferenceError occurs and further expressions
            >>> are not evaluated.[/color]
            >>
            >> Is it possible to determine that this (ReferenceError ) has occurred,[/color]
            >
            > Yes, if the error reporting feature of your UA does not suffice,
            > you could use
            >
            > try
            > {
            > // statements that may cause errors
            > }
            > catch (e)
            > {
            > alert(e.name + ": " + e.message);
            > }
            >
            > provided it is supported (ECMAScript 3 compliant implementation) .
            > Or use proprietary
            >
            > onerror = function()
            > {
            > alert("Error!") ;
            > onerror = null;
            > return true;
            > }[/color]

            Thinking about it, if prompt() does not work, alert() may not work either,
            as both are methods of the Window host object and not built-in language
            features.

            Maybe you will have to find other ways to indicate error. You could change
            the value of a variable, for example. Comparison will at least indicate
            that there was an error. I think, _which_ error occured cannot be easily
            determined without using AOM/DOM features.


            PointedEars

            Comment

            Working...