OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Keypress handler for the textarea. Sends the textarea value |
| 7 * to the HTTP server when "Enter" key is pressed. |
| 8 * @param {Event} event The keypress event. |
| 9 */ |
| 10 function handleTextareaKeyPressed(event) { |
| 11 // If the "Enter" key is pressed then process the text in the textarea. |
| 12 if (event.which == 13) { |
| 13 var testTextVal = document.getElementById('testtext').value; |
| 14 var postParams = 'text=' + testTextVal; |
| 15 |
| 16 var request = new XMLHttpRequest(); |
| 17 request.open('POST', 'keytest/test', true); |
| 18 request.setRequestHeader( |
| 19 'Content-type', 'application/x-www-form-urlencoded'); |
| 20 |
| 21 request.onreadystatechange = function() { |
| 22 if (request.readyState == 4 && request.status == 200) { |
| 23 console.log('Sent POST request to server.'); |
| 24 } |
| 25 }; |
| 26 |
| 27 request.onerror = function() { |
| 28 console.log('Request failed'); |
| 29 }; |
| 30 |
| 31 request.send(postParams); |
| 32 } |
| 33 } |
| 34 |
| 35 window.addEventListener( |
| 36 'load', |
| 37 function() { |
| 38 document.getElementById('testtext').addEventListener( |
| 39 'keypress', |
| 40 handleTextareaKeyPressed, |
| 41 false); |
| 42 }, |
| 43 false); |
| 44 |
OLD | NEW |