我在index.html中使用提示请求用户名。我的代码如下所示:
<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访问此变量?我会以某种方式将其发回给该文件吗?
答案 0 :(得分:0)
您可以尝试将其存储在会话变量中,然后从任何地方访问它
希望这会对你有所帮助 感谢!!!