SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using Conditional Statements in Functions

    Good evening everyone,

    I am practicing, well doing things the wrong way mostly.

    I have a very simple script that generates a prompt window.

    Instead of using a default text, I have left the text field empty.

    However, I would like a default message to display in the pop up window if the user does not enter a value in the prompt box before clicking okay.

    I tried adding a conditional statement but the default text does not write in the pop up window.

    Here is the code:
    Code:
    function promptBox(){
    	 var message = prompt("Who is your favorite Hollywood Star?", "");	
         newWindow = window.open('','','width=800,height=600');
         newWindow.document.write(message);
         newWindow.focus();
         
          if ((prompt) == null);
    	 document.write("No Value Entered");
    
         
         }
    I'll do more reading when I get home.



    Thanks everyone!!


    Novice2010

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
          if ((prompt) == null);
    	 document.write("No Value Entered");
    prompt is a function name not the returned value.
    Code:
          if (message == "" )
    	 document.write("No Value Entered");
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    prompt is a function name not the returned value.
    Code:
          if (message == "" )
    	 document.write("No Value Entered");
    Hello there and thanks for your help!

    The problem is the message or default text needs to appear in the pop up window after the user click okay. Not the page with the button.

    Here is the complete code:

    Code:
    <script type="text/javascript">
    
    function promptBox(){
        
         var message = prompt("Who is your favorite Hollywood Star? ", "");
         if (message == "")
         document.write("No Value Entered");
         myWindow = window.open('','','width=800,height=600');
         myWindow.document.write(message);
         myWindow.focus();
    }
    //-->
    </script>
    
    
    </head>
    
    <body>
    <input type="button" value="Prompt" onclick="promptBox()" />
    </body>
    I am just confused as to where the condition should go so that the default text gets to the pop up page/window.

    Novice

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,603
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Code:
    function promptBox(){
        
         var message = prompt("Who is your favorite Hollywood Star? ", "");
         if (message == "")
              message = "No Value Entered";
         myWindow = window.open('','','width=800,height=600');
         myWindow.document.write(message);
         myWindow.focus();
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Code:
    function promptBox(){
        
         var message = prompt("Who is your favorite Hollywood Star? ", "");
         if (message == "")
              message = "No Value Entered";
         myWindow = window.open('','','width=800,height=600');
         myWindow.document.write(message);
         myWindow.focus();
    }
    After writing all the content, it's also important to call myWindow.close() .
    Tab-indentation is a crime against humanity.

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Code:
    function promptBox(){
        
         var message = prompt("Who is your favorite Hollywood Star? ", "");
         if (message == "")
              message = "No Value Entered";
         myWindow = window.open('','','width=800,height=600');
         myWindow.document.write(message);
         myWindow.focus();
    }
    Thanks guys,

    Felgall, as always, you are the very best!

    Novice


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
  •