views:

160

answers:

2

I'm rolling my own version of prompt() for aesthetic purposes; it's come along quite nicely as far as visuals go, but I have run into a slight hitch: the native version of the function causes code execution to cease completely until the prompt has been dealt with.

This is positively lovely and it's why the below works the way it does:

<script>
var c = prompt('Name?', '');
alert(c); // displays whatever the user entered
</script>

With my method, however, things do not go as smoothly. I am using a dialog, an input box, and an "OK" button to gather the data from the user; to my knowledge, data collection works perfectly; that is, I know for sure that after the user presses the "OK" button, I have access to the data they just put into the prompt.

I cannot, however, find a way to get my version to work as the native one does. My question, then, is this: is it at all possible to tell JavaScript to halt executing until you've told it to resume?

Thanks in advance for any and all assistance.

+5  A: 

No, it is not possible to duplicate this behavior. The way to achieve the same effect is to use a callback in your code, so you can do something like:

myPrompt('Hello, mate, whats yer name?', function(answer) {
   alert(answer);
});

EDIT: Based on your code, why not do this?

<body>
<div id="prompt" style="display: none;">
<input type="text" id="q" /> <input type="button" value="OK" id="ok" />
</div>
<script>
$ = function(i) {return document.getElementById(i);}

_prompt = function(prompt, callback) {
    $('prompt').style.display = '';
    $('q').value = '';
    $('ok').onclick = function() {
        callback($('q').value);
    }
}

_prompt('Name?', function(answer) {
    alert(answer);  
});
</script>
</body>

If you change alert(answer); to say... gAnswer = answer; (notice no var declaration) you would be creating a global variable named gAnswer that you could access anywhere else in the javascript code, assuming the prompt was already answered. If you're concerned of global variables polluting your space you could wrap it all in a closure, but it should be fine otherwise.

Paolo Bergantino
Ack! Just as I figured... so there's no way to store the value for later use, then, huh?
Hexagon Theory
You could, what are you trying to accomplish? If you make the variable global and set it inside the callback you could access it later.
Paolo Bergantino
That's the thing, mate... you can't. : (
Hexagon Theory
Yes you can... can you post what you are trying?
Paolo Bergantino
You can code myPrompt() to return the value that the user input. As a result, you'll have `var c = myPrompt(...)`, and you can then use `c` anywhere else without resorting to global variables.
Ron DeVera
While definitely an improvement, your implementation still doesn't allow me to access the answer in some future portion of my code... erm, does it?
Hexagon Theory
+1 love your first example
alex
A: 

@Paolo:

This is the code I am currently working with:

<body>
<div id="prompt" style="display: none;">
<input type="text" id="q" /> <input type="button" value="OK" id="ok" />
</div>
<script>
$ = function(i) {return document.getElementById(i);}
_prompt = function(q, e)
 {
    $('prompt').style.display = '';
    $('q').value = '';
    $('ok').setAttribute('onclick', e + ' $("prompt").style.display = "none";');
 }
var c; _prompt('Name?', 'c = $("q").value;');
alert(c);
</script>
</body>

Now, as would be expected, that alert() fires as soon as the page is loaded, which is most definitely not what I want; ideally, I'd like for the rest of the code to wait for the prompt to get handled, but I'm strongly doubting this is possible outside of the native implementation. Reckon I'll just have to settle for designing my algorithm so that the prompt gets used immediately?

Hexagon Theory