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:

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

Thanks guys,

Felgall, as always, you are the very best!

Novice

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:

<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

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();
}
      if ((prompt) == null)[COLOR="Red"];[/COLOR]
	 document.write("No Value Entered");

prompt is a function name not the returned value.

      if (message == "" )
	 document.write("No Value Entered");

After writing all the content, it’s also important to call myWindow.close() .