Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1667 Is it possible to prompt the user for two values?

You are here: irt.org | FAQ | JavaScript | Window | Q1667 [ previous next ]

You can either prompt the user twice, for example:

<script language="JavaScript"><!--
var userid = prompt('Userid','');
var password = prompt('Password','');
alert('userid = ' + userid + '\npassword = ' + password);
//--></script>

Or you can create your own popup window, for example:

<script language="JavaScript"><!--
var userid = '';
var password = '';
var windowReference;

function promptForTwo() {
  var w = 480, h = 340;

  if (window.screen) {
    w = screen.availWidth;
    h = screen.availHeight;
  }

  var popW = 300, popH = 150;
  var leftPos = (w-popW)/2, topPos = (h-popH)/2;

  window.open('popup.htm','windowName','width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos);

  if (!windowReference.opener)
    windowReference.opener = self;
}

function done() {
  alert('userid = ' + userid + '\npassword = ' + password);
}

promptForTwo();
//--></script>

And then in popup.htm:

<html>

<head>

<script language="JavaScript"><!--
function prompts(form) {
  opener.userid = form.userid.value;
  opener.password = form.password.value;
  opener.done();
  self.close();
  return false;
}

function cancel() {
 self.close();
  return false;
}
//--></script>

</head>

<body onLoad="document.myform.userid.focus()">

<form name="myForm" onSubmit="return prompts(this)" onReset="cancel()">
Userid: <input type="text" name="userid">
<br>
Password: <input type="password" name="password">
<br>
<input type="submit" value="  ok  "> <input type="reset" value="  cancel  ">
</form>

</body>

</html>

©2018 Martin Webb