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

  • VK

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

    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.getEle mentById("sysOu t");

    myName = prompt("Please enter your name:","");

    if (myName) {
    sysOut.innerHTM L = "Your name is: " + myName;
    }
    else {
    sysOut.innerHTM L = "You did not tell me your name... :-(";
    }
    }
    </script>

    </head>

    <body onload="myFunct ion()">

    <div id="sysOut">Di d it work out ok?</div>

    </body>

    </html>

    Comment

    • joemac

      #3
      Re: prompt not working

      >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.
      [color=blue]
      >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.[/color]

      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

      Comment

      • VK

        #4
        Re: prompt not working

        joemac wrote:[color=blue]
        > 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... :-([/color]

        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.

        Comment

        • joemac

          #5
          Re: prompt not working

          VK wrote:[color=blue]
          > joemac wrote:[color=green]
          > > 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... :-([/color]
          >
          > It means then that
          >
          > Option A) You browser doesn't support JavaScript - highly unlikely
          > but...[/color]

          I'm using IE version 6. Seems unlikely to me as well.
          [color=blue]
          > 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.[/color]

          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

          Comment

          • VK

            #6
            Re: prompt not working


            joemac wrote:[color=blue]
            > Any other ideas?[/color]

            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.

            Comment

            • joemac

              #7
              Re: prompt not working

              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:[color=blue]
              > joemac wrote:[color=green]
              > > Any other ideas?[/color]
              >
              > 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.[/color]

              Comment

              • RobG

                #8
                Re: prompt not working

                joemac wrote:[color=blue]
                > 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.[/color]

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

                Comment

                • joemac

                  #9
                  Re: prompt not working

                  Lee wrote:[color=blue]
                  >
                  > Since you know that you should use "text/javascript", why did you
                  > post code with the obsolete form, instead?[/color]

                  I *don't* know which form of usage is correct. According to the text
                  that I'm using ("Introducti on to Interactive Programming on the
                  Internet using HTML & JavaScript", by Craig D. Knuckles), "<script
                  language="JavaS cript">" 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.
                  [color=blue]
                  > 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>[/color]

                  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

                  Comment

                  • joemac

                    #10
                    Re: prompt not working

                    RobG wrote:[color=blue]
                    > joemac wrote:[color=green]
                    > > 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.[/color]
                    >
                    > Please don't top post, reply below quoted text and trim unnecessary
                    > material.[/color]

                    Ok.
                    [color=blue]
                    > 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>Wr ite test</title></head>
                    > <body>
                    > <script type="text/javascript">
                    > var aName = prompt('Enter your name please','');
                    > document.write( aName);
                    > </script>
                    > </body>
                    > </html>[/color]

                    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.
                    [color=blue]
                    > If that doesn't work, Google 'mark of the web'.[/color]
                    [color=blue]
                    >From what I just read about it it seems to only be an issue in Windows[/color]
                    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

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: prompt not working

                      joemac wrote:
                      [color=blue]
                      > Lee wrote:[color=green]
                      >> Since you know that you should use "text/javascript", why did you
                      >> post code with the obsolete form, instead?[/color]
                      >
                      > I *don't* know which form of usage is correct.[/color]

                      Bad enough.
                      [color=blue]
                      > According to the text that I'm using ("Introducti on to Interactive
                      > Programming on the Internet using HTML & JavaScript", by Craig D.
                      > Knuckles), "<script language="JavaS cript">" is the correct form of
                      > usage.[/color]

                      Burn it. Obviously its content is either completely wrong or badly
                      outdated. (The HTML 4.01 Specification is dated December 24, _1999_).
                      [color=blue]
                      > 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.[/color]

                      <URL:http://validator.w3.or g/> 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

                      Comment

                      • RobG

                        #12
                        Re: prompt not working

                        joemac wrote:[color=blue]
                        > RobG wrote:[/color]
                        [...][color=blue][color=green]
                        >><html>
                        >> <head><title>Wr ite test</title></head>
                        >> <body>
                        >> <script type="text/javascript">
                        >> var aName = prompt('Enter your name please','');
                        >> document.write( aName);
                        >> </script>
                        >> </body>
                        >></html>[/color]
                        >
                        >
                        > 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.[/color]

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

                        [color=blue][color=green]
                        >>If that doesn't work, Google 'mark of the web'.[/color]
                        >
                        >[color=green]
                        >>From what I just read about it it seems to only be an issue in Windows[/color]
                        > 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.[/color]

                        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

                        Comment

                        • joemac

                          #13
                          Re: prompt not working

                          Thomas 'PointedEars' Lahn wrote:[color=blue]
                          >
                          > <URL:http://validator.w3.or g/> pretty much tells you what is correct
                          > regarding HTML and what is not.[/color]

                          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

                          Comment

                          • Thomas 'PointedEars' Lahn

                            #14
                            Re: prompt not working

                            joemac wrote:
                            [color=blue]
                            > Thomas 'PointedEars' Lahn wrote:[color=green]
                            >> <URL:http://validator.w3.or g/> pretty much tells you what is correct
                            >> regarding HTML and what is not.[/color]
                            >
                            > 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.[/color]

                            Be sure you also serve it with the correct Content-Type HTTP header.
                            <URL:http://web-sniffer.net/>
                            [color=blue]
                            > [...] 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">[/color]

                            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>
                            [color=blue]
                            > <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.","");[/color]

                            That is the same as

                            var myName = prompt("Please enter your name.", "");
                            [color=blue]
                            > document.write( "My name is: ", myName);[/color]

                            Try

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

                            instead.
                            [color=blue]
                            > </SCRIPT>
                            > </HEAD>
                            > <BODY>
                            > <p>Did it work out ok?[/color]

                            Even if not required in HTML 4.01 Transitional, you should
                            include the close tag </p>.
                            [color=blue]
                            > </BODY>
                            > </HTML>[/color]

                            (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

                            Comment

                            Working...