SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist FCC's Avatar
    Join Date
    May 2006
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Prompt Script weirdness

    Hi All,

    I was testing this code someone gave me on the forum earlier, and I am having a hard time understanding why I get this weird result:

    Code:
    <SCRIPT language="JavaScript"> 
    <!--hide  
     var yourname= prompt('Please enter your name, so you can get a special greeting', ' ');
    
     if ( (yourname==' ') || (yourname==null) ) 
     { 
       yourname="Dude"; 
     } 
    
    //--> 
    </SCRIPT> 
    
    </HEAD> 
    
    <BODY> 
    
    <SCRIPT language="JavaScript"> 
    <!--hide 
     document.write("<CENTER><H1>Hello, " + yourname + " ! Welcome to My Page! <\/H1><\/CENTER>");
    
    //--> 
    </SCRIPT> 
    
    </BODY>
    If the user presses ok with nothing entered, or presses cancel your name is changed to dude.

    Now I did a little modification on the code
    Code:
    <SCRIPT language="JavaScript"> 
    <!--hide  
     var yourname= prompt('Please enter your name, so you can get a special greeting', ' ');
    
     if ( yourname==' ')  
     { 
       yourname="Dude"; 
     } 
    
    //--> 
    </SCRIPT> 
    
    </HEAD> 
    
    <BODY> 
    
    <SCRIPT language="JavaScript"> 
    <!--hide 
     document.write("<CENTER><H1>Hello, " + yourname + " ! Welcome to My Page! <\/H1><\/CENTER>");
    
    //--> 
    </SCRIPT> 
    
    </BODY>
    I just took out the (yourname==null), so that the only parameter in the if statement is yourname==' '. WHich means the name is changed to "dude" only if the user presses cancel, and not when they press ok with nothing entered. Yet when I try this code, I continue to get null for the name now.

    What is going on?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,998
    Mentioned
    186 Post(s)
    Tagged
    2 Thread(s)

    null dude

    I just tried your modified version in IE and Firefox. Both give "dude" for OK and "NULL" for cancel.


Bookmarks

Posting Permissions

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