By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,261 Members | 1,674 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,261 IT Pros & Developers. It's quick & easy.

Undefined on HTTPRequest

computerfox
100+
P: 278
Hello everyone,

I have a small question. Here's my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head>
  4.         <title>Test</title>
  5.         <script>
  6.                 function show(id){
  7.                         var file="test.txt";
  8.                         alert(file_get_contents(file));
  9.                 }
  10.                 function file_get_contents(path){
  11.                         var httpRequest;
  12.                         if (window.XMLHttpRequest) {
  13.                                 httpRequest = new XMLHttpRequest();
  14.                         }
  15.                         else if (window.ActiveXObject) { // IE 8 and older
  16.                                 httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  17.                         }
  18.                         httpRequest.onreadystatechange = function(){
  19.                                 if (httpRequest.readyState === 4) {
  20.                                         if (httpRequest.status === 200) {
  21.                                                 return httpRequest.responseText;
  22.                                         }
  23.                                 }
  24.                         };
  25.                         httpRequest.open('GET',path);
  26.                         httpRequest.send();
  27.                 }
  28.         </script>
  29.  
  30. </head>
  31. <body>
  32.         <form enctype="multipart/form-data" >
  33.                 <input type="file" width="100%" id="file"/><br/>
  34.                 <button type="button" onclick="show('file')" width="100%" id="submit">Show</button>
  35.         </form>
  36. </body>
  37. </html>
  38.  
  39.  
For some reason I get undefined at the alert. Am I missing something?

Thank you. FOX
Apr 19 '13 #1
Share this Question
Share on Google+
2 Replies


Rabbit
Expert Mod 10K+
P: 12,430
That's because your file_get_contents function doesn't return anything. The function you assigned to the onreadystatechange event is returning data. But not the function that you tried to echo.
Apr 19 '13 #2

computerfox
100+
P: 278
I just noticed that.... Thanks Rabbit....
Apr 20 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.