/    Sign up×
Community /Pin to ProfileBookmark

Pre-writing prompt boxes, then running them later – possible or not?

Hey, thanks for coming. Basically, my question is this: can you define a prompt box at the [I]beginning[/I] of a program, and then run it later?

I know that may seem odd, but in my program, the popup appears as the result of several possible actions the user can perform. It seems like it would be nice to not have to write out the same prompt box for each possible action.

Let me know if that doesn’t make sense; if it doesn’t, I’ll see if I can find another way to describe it.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@NandraauthorJul 31.2008 — I'll bump this back up to the top of the list...
Copy linkTweet thisAlerts:
@slaughtersJul 31.2008 — Which probably means it did not make sense and you need to see if you can find another way to describe it. ?

What "prompt box" - the alert function ?
Copy linkTweet thisAlerts:
@NandraauthorJul 31.2008 — Oh, okay - thanks for letting me know. ^_^

A "prompt box" is a pop-up that allows the user to input text. For instance, it might say...
[CODE]var name=prompt("Please enter your name here","Default")[/CODE]
...and then a box will come up telling you to enter your name, with default entered in the text field.

As for the rest of it -

I guess it's kind of like defining a function. At the beginning of the document, I want to define a function that activates a prompt box, and then run that function later.

The problem is, a variable in a function can't be used outside of that function. So I'm trying to find a way to circumvent that problem.

Does that make a little more sense? -fingers crossed-


Maybe I should explain a little more about the program, so that there's a context... I'm working on a program that I guess could be called a "choose-your-own adventure" - there are several options, each of which lead to another set of options, and so on. However, each person should hit the same events, even if in a different order - and as it's operated exclusively by prompt boxes, that's where the problem comes in. The best thing would be if a "popup.run" code, or something like that, existed; that way, I could just define them once, rather than writing them out four different times.
Copy linkTweet thisAlerts:
@slaughtersJul 31.2008 — Here is a very straight forward brute force way of doing it:

cons: cumbersome as heck and hard to maintain if there are a *lot* of questions

pros: easy understand for people unfamiliar with JavaScript

[code=html]<script>
var name = "";
var age = "";
var sex = "";

function Ask(Type,Question) {
if (Type == "name") name = prompt(Question);
if (Type == "age") age = prompt(Question);
if (Type == "sex") sex = prompt(Question);
}
</script>
.
.
.
<a href="#" onClick="Ask('name','Please Enter your name');">Answer Name Question</a><br>
<a href="#" onClick="Ask('age','Please Enter your age');">Answer Age Question</a><br>
<a href="#" onClick="Ask('sex','Please Enter your sex');">Answer Sex Question</a><br>
.
.
.[/code]
Copy linkTweet thisAlerts:
@NandraauthorJul 31.2008 — Unfortunately, there [I]are[/I] a lot of questions, but I think this way will still be way less complicated than what I was previously doing. ?

Thanks a lot! I think this might be just what I was looking for.
Copy linkTweet thisAlerts:
@JMRKERAug 01.2008 — I'm not sure what you want to do. ?

My assumption below is that you want to ask a number of questions and keep the responses,

but the questions should be presented in a random order and all questions should be asked.

The following does that. ?

All the resonses are saved in the order of the questions even if the presentation order is scrambled.


So the response to the first question is stored in the first element of the response array. ?

[code=php]
<html>
<head>
<title>Random Prompts</title>
<script type="text/javascript">
// For: http://www.webdeveloper.com/forum/showthread.php?t=187768

var Questions = [
"Please enter your name",
"Please enter your age",
"Please enter your gender",
"Are you married?",
"Do you own your house?",
"What level of education (HS,College,Postgrad,Proffessiona)?"
];
var Responses = [];
function randOrd(){
return (Math.round(Math.random())-0.5);
}
var Question_Order = [];
function randomize_questions() {
for (var i=0; i<Questions.length; i++) {
Question_Order[i] = i;
Responses[i] = '';
}
Question_Order.sort(randOrd);
}
function Ask(QNo) {
var Response = prompt(Questions[QNo]);
Responses[QNo] = Response;
}
function StartAdventure() {
for (var i=0; i<Question_Order.length; i++) {
Ask(Question_Order[i]);
}
}
</script>
</head>
<body onload="randomize_questions()">
<button onclick="StartAdventure()">Start Adventure</button>
<button onClick="alert(Responses.join('n'))">Show Responses</button>
</body>
</html>
[/code]


I think the underlying concept could be applied to an 'adventure game' if you want,

but you would need to give a better description of your desired outcome.

Anyway, use it if you want and Good Luck! ?
Copy linkTweet thisAlerts:
@NandraauthorAug 01.2008 — Oh, that was awfully nice of you! Thanks! ?

Unfortunately, it's a little different from the project I'm working on - sorry for not really explaining that well. Mine actually is a kind of "adventure game", but it's kind of different...

The game is kind of like a text-based easter-egg hunt, in a way: the user is trying to locate several different hidden things, and gets to choose where to look, through the prompt boxes. Eventually, though, they should work their way through all of the possible locations.

I hope that makes sense! I'm not really all that great at describing things like this, in case you can't tell. :o

Thanks so much for writing that up for me, though, I really appreciate it! Even if it isn't applicable to this program, it's really cool to know how to do that.
×

Success!

Help @Nandra spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...