0
私はindex.html内でプロンプトを使用してユーザー名を要求します。私のコードは次のようになります:index.htmlとapp.jsの間の変数
<script>
// YOU DO NOT NEED TO EDIT THIS CODE
if (!/(&|\?)username=/.test(window.location.search)) {
var newSearch = window.location.search;
if (newSearch !== '' & newSearch !== '?') {
newSearch += '&';
}
var username = prompt('What is your name?') || 'anonymous';
console.log("From Index.html:" , username);
newSearch += 'username=' + (username);
window.location.search = newSearch;
}
</script>
私のapp.jsでこのユーザー名にアクセスする必要があります私はプロパティとしてユーザー名が必要なサーバーにPOST要求を送信しています。たとえば:
//POST the message to the server
handleSubmit: function() {
var query = window.location.search;
var username = query.slice(10, query.length);
var room = $('#roomSelect').find(':selected').text();
var msg = {
username: username,
text: $('#message').val(),
roomname: room
};
app.send(msg);
console.log(msg);
app.renderMessage(msg);
}
は基本的に、私はindex.htmlの中で自分のユーザー名をユーザーに要求したいが、どのように私はapp.jsからこの変数にアクセスできますか?私はそれを何とかそのファイルに送り返しますか?
両方のjsを同じページに含めて、正しい順序でトリックする必要があります... – SubjectDelta