www.webdeveloper.com
Results 1 to 8 of 8

Thread: Javascript Prompt Wrong Outcome?

  1. #1
    Join Date
    Aug 2008
    Posts
    31

    Javascript Prompt Wrong Outcome?

    Hello, I want to be able to click a link and have a input prompt pop up followed by an alert and then right the contents of the prompt to a specific spot. heres my code so far:

    Code:
    <html>
    <head>
    
    <script language="JavaScript">
    function firstName() {
    var name = prompt ("Please Enter Your Name");
    alert('blah blah blah');
    }
    </script>
    
    </head>
    <body>
    
    <a href="#blah" onClick="firstName()">Click</a>
    
    <script language="JavaScript">document.write(name);</script>
    
    </body>
    </html>
    but my problem is when I type something in, and hit ok on the prompt and ok on the alert, the output is "m" and then some random numbers. can i possibly fix this?
    Last edited by bholtiphone; 11-18-2008 at 11:20 PM.

  2. #2
    Join Date
    Nov 2008
    Posts
    3
    Why do you not use document.write in same function like

    <html>
    <head>

    <script language="JavaScript">
    function firstName() {
    var name = prompt ("Please Enter Your Name");
    alert('blah blah blah');
    document.write(name);
    }
    </script>

    </head>
    <body>

    <a href="#blah" onClick="firstName()">Click</a>

    </body>
    </html>

  3. #3
    Join Date
    Aug 2008
    Posts
    31
    how would I specify where it writes to?

  4. #4
    Join Date
    Oct 2008
    Posts
    349
    Three problems

    1) Unless specified otherwise, all scripts are run when the page is loaded, so your document.write would actually execute before user get a chance to click on the link.

    2) Your onclick doesn't actually cancel the default hyperlink-click action, which is to reload the page at "#blah". (Meaning anything done on this page is naught) You need to change it to
    Code:
    <a href="#blah" onClick="firstName(); return false;">Click</a>
    3) I am not sure if you want to use document.write, it is a very specialized method that is mostly used for ad syndication and pop-ups. Not designed for dynamic webpages.

  5. #5
    Join Date
    Aug 2008
    Posts
    31
    ok is it possible to do this any other way then? i want a link that goes to an anchor and when clicked a prompt and then an alert should pop up. then the contents of the prompt would be displayed somewhere i specify. and i would like to do this all in javascript.

    thanks

  6. #6
    Join Date
    Aug 2007
    Posts
    3,767
    The only problem is using document.write. If you use innerHTML instead, you'll be singing.
    Code:
    <a href="#blah" onClick="firstName(); return false;">Click</a>
    Code:
    <div id="blah"></div>
    Put that div where ever you want the name to appear, and it is also where the page will jump to. Change the id (blah) to something else if you want to change that behaviour.
    Code:
    <script type="text/javascript">function firstName() {
    var name = prompt ("Please Enter Your Name");
    document.getElementById("blah").innerHTML = name;
    }</script>
    And a quick note, a link with "#blah" as the href doesn't actually reload the page, just jumps down along it.
    Great wit and madness are near allied, and fine a line their bounds divide.

  7. #7
    Join Date
    Aug 2008
    Posts
    31
    Thank you!! one question though. is it possible to output this to multiple places on the page? because i tried and it only showed up on one of the spots i put it

  8. #8
    Join Date
    Aug 2008
    Posts
    31
    Never mind i figured it out.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles