如果用户说出正确的响应,则加载其他文件

时间:2016-06-01 02:40:13

标签: javascript

在线上,34-41我试图这样做,如果用户输入游戏,它将自动加载来自不同文件的游戏,并且同样用于谈话。我怎么能用js做到这一点?

var storeUsersInfo = [];
var amountOfUsers = prompt("How many users do you want?");
amountOfUsers = parseInt(amountOfUsers);
function returnUserInput() {
    var askFirstName = prompt("What is your first name?");
    var askLastName = prompt("What is your last name" + " " +     titleCase(askFirstName) + "?");

    while(true) {
        var askAge = prompt("How old are you" + " " + titleCase(askFirstName) + " " + titleCase(askLastName) + "?");
        if(Number.isInteger(Number.parseInt(askAge))) break;
        alert("Not a valid input, please enter your response as a number.");
    };

    return {
        firstName: titleCase(askFirstName),
        lastName: titleCase(askLastName),
        age: askAge
    };
};

function titleCase(string) {
    return string.charAt(0).toUpperCase() + string.slice(1); 
};

for(var i = 0; i < amountOfUsers; i++) {
    storeUsersInfo[i] = returnUserInput();
}

console.log("Your information has been stored in the object below.");
console.log(storeUsersInfo);

var askUserToDoSomethingElse = prompt("Do you want to do something else?"); // Yes or No

    if(askUserToDoSomethingElse = "yes") {
        var chooseSomethingElse = prompt("If you want to play a game type game, or if you just want to talk type talk.");
            if(chooseSomethingElse === "game") {
                alert("Okay!");
            } else if (chooseSomethingElse === "talk") {
                alert("Okay!");
            }
    };

1 个答案:

答案 0 :(得分:0)

您有两种选择:

1。) XMLHttpRequest

    var client = new XMLHttpRequest();
    client.open('GET', '/<fileName>');
    client.onreadystatechange = function() {
       //do something with your file
    }

2.)使用Jquery

    jQuery.get('http://localhost/foo.txt', function(data) {
        //file loaded in data. Now you can use it. 
    });