Javascript if else

if statement problem..

Page 1 of 1

1 Replies - 3477 Views - Last Post: 26 February 2009 - 04:28 PM

#1 4x4pirate  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 18-October 06

Javascript if else

Posted 26 February 2009 - 09:46 AM

Trying to make an if else statement..

This is just a silly prompt to enter your name.., when you enter it.. a web page navigates to a blank page and says "Hi there name" but if someone doesn't enter anything in the prompt, it doesn't do anything and just closes the prompt.


function disp_prompt()
{
var name=prompt("Please enter your name","");
if (name!=null && name!="")
  {
  document.write("Hi There " + name + "!");
  }
}





I am trying to take it one step futher, and instead of disturbing the main page, open it in a new window, which I have done.. the problem is, if you don't enter anything, a blank window pops up... which I don't want to happen.


function disp_prompt()
{
var name=prompt("Please enter your name","");
var win = window.open("", "win", "width=300,height=200"); // a window object
if (name!=null && name!="")
  {
win.document.open("text/html", "replace");
win.document.write("Hi There " + name + "!");  //This allows a new window to open without disturbing the main page
win.moveTo(600,400)  // 
win.document.close();

}
  
}






I am assuming it should look like this.. but not 100%

[code]function disp_prompt()
{
var name=prompt("Please enter your name","");
var win = window.open("", "win", "width=300,height=200"); // a window object
if (name!="")
  {
win.document.open("text/html", "replace");
win.document.write("Hi There " + name + "!");  //This allows a new window to open without disturbing the main page
win.moveTo(600,400)  // 
win.document.close();
{
else if (name!=null)
//do nothing
}

{
{  




Any help is appreciated!

This post has been edited by 4x4pirate: 26 February 2009 - 09:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Javascript if else

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,207
  • Joined: 07-September 06

Re: Javascript if else

Posted 26 February 2009 - 04:28 PM

You are telling the window to open up either way, change the code to look like so:

function disp_prompt()
{
var name=prompt("Please enter your name","");
// window.open opens a new window, even if it isn't used for anything else
if (name!=null && name!="")
  {
var win = window.open("", "win", "width=300,height=200"); // a window object
win.document.open("text/html", "replace");
win.document.write("Hi There " + name + "!");  //This allows a new window to open without disturbing the main page
win.moveTo(600,400)  // 
win.document.close();

}
  
}


I haven't tested it, so it may not work perfectly, but I think it will work.

HTH
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1