Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

custom input box 2

Status
Not open for further replies.

snotmare

Programmer
Jan 15, 2004
262
US
Greetings!

I am trying to mimic the VBScript input box by creating my own InputBox() function. In this function, I will simply display a <DIV> in the middle of the screen that contains a header, a message, a text box for input, an "OK" button, and a "Cancel" button.

My problem is this. When I call the InputBox() function, I want to pause execution so that I can wait for the user to input a value into the text box. If I have the following code:
Code:
alert(InputBox("My heading", "My message"));

function InputBox(strHeading, strMessage){
   //do code to set heading and message values
   //do code to display <DIV>
   
   while([ok button not pressed] && [cancel button not pressed]){
       wait(3000) //<-- I need this code here!
    }

    return [inputed message text];
}

The wait command needs to suspend execution so that the alert box does not display until the user clicks ok or cancel.

I've played around with the setTimeout function, but that still allows execution to continue in the original routine.

What is the javascript method to pause execution?
Has anyone tried to do something like this before?
What are your expert ideas?

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
you may want to look into javascript's showModalDialog method, although this will work only in internet explorer. it will allow you to open a window of your choice, and force the user to take some action in the window before allowing him to move elsewhere in the calling page.



*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Will that suspend execution from the calling page? If so, then that will definately work. I would just have to figure out how to pass values back and forth.

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
Hey, that looks like it could work! Thanks a ton!


---------------------
He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
Actually, I'd still like to know if there is a way to pause execution. Does javascript have some sort of a wait or pause function?


---------------------
He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
Thank you for your response fendal, but from my first post, you'll notice that setTimout does not stop execution, it simply indicates that a specific function second will execute at a given time while the original execution still continues.

I am looking for code to simply pause execution for a given amount of time without calling another function.


---------------------
He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
So... you will need to rework your code. Personally, I'd have the message box as an object. with a handler that you attach a function to, that will be called when the user has finished with the box.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I thought about using an object, but I'm still not sure how I can stop execution of the calling code. What exactly do you mean by a handler? Will that suspend execution of the calling code while the user inputs values? I'll try to find info on "handlers" in the meantime.

Thanks for your help guys, I learn something every day here!


---------------------
He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
snotmare said:
I thought about using an object, but I'm still not sure how I can stop execution of the calling code.

I'm not suggesting stopping execution of the code... I'm suggesting reworking it so you don't need to.

Whether or not you convert your message box to an object is a harengs rouges... You do not have do to so for the method I'm suggesting to work.

Here's the code in a nutshell:

1. Have a variable, initially set to "null":

Code:
var myCallBackFunction = null;

2. Call your messagebox function as usual. This does nothing more than get the required input from the user.

3. After the input have been received, test to see if the myCallBackFunction variable is not null (or whether its type is 'function'). If so, call it with the value you gained from the message box:

Code:
if (myCallBackFunction != null) myCallBackFunction(yourValueHere);

The function can then do the alert, or whatever.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Interesting. I'll go down that path a bit and see what I come up with. Thank you for your creativity!


---------------------
He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
Maybe I am misunderstanding your question, in which case feel free to ignore this.
Code:
<html><body>
<script type="text/javascript">
prompt('I will wait until you type something','Waiting')
confirm('Is that what you need?')
alert("Ok then");
</script>
</body></html>

Clive
 
If you're waiting for the OK or Cancel buttons to be pressed, why not just set the onclick events of those buttons to perform an action?

Here's a way to SIMULATE pausing 5 seconds:

Code:
//WITHOUT the pause:

function myFunction()
{
 window.status = "Hey!";
 var x = document.getElementById("myElement").value
 for(var i=0; i<x; i++)
 {
  window.status += "!";
 }//end for
}//end myFunction()

//WITH a pause:
function myFunction1()
{
 window.status = "Hey!";
 var x = document.getElementById("myElement").value;
 setTimeout("myFunction2("+x+");", 5000);
}//end myFunction1()

function myFunction2(val)
{
 for(var i=0; i<val; i++)
 {
  window.status += "!";
 }//end for
}//end myFunction2()

Is that what you're after? Obviously, it requires calling a function, but if you break the function in "half" and properly send the parameters you need, you can simulate the pause you're after.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top