2017-10-02 5 views
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からこの変数にアクセスできますか?私はそれを何とかそのファイルに送り返しますか?

+0

両方のjsを同じページに含めて、正しい順序でトリックする必要があります... – SubjectDelta

答えて

0

あなたは、これはあなたに 感謝を助けることがセッション変数に格納し、どこでも

希望からアクセスしようとすることができます!

関連する問題