Fetching data from a file

Discussion in 'JavaScript' started by Jzqkhr, May 13, 2009.

  1. #1
    Hello, I'm looking for some help here.
    The problem is I'm not very good with javascript, I try to avoid using it as much as I can, but ther's a project that I'm working on that requires some things done specifically by javascripts.

    The problem is very basic, but hard for me as I dont really understand this language: I would be very grateful if someone would write me an example of a function that would fetch content from a file in the web (e.g. http://something.com/something.txt) and write it to a variable. The file would consist only of a few simbols.
    Thank you in advance for any answers :)
     
    Jzqkhr, May 13, 2009 IP
  2. X-N2O

    X-N2O Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here it is in PHP:
    
    <?php
    $variable = file_get_contents('http://www.google.com');
    echo $variable
    ?>
    
    PHP:
     
    X-N2O, May 13, 2009 IP
  3. Jzqkhr

    Jzqkhr Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but I know how to do this on php, the problem is I MUST do that using javascript. Unless you know how to make firefox extensions work with php ^^
     
    Jzqkhr, May 13, 2009 IP
  4. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #4
    rohan_shenoy, May 14, 2009 IP
  5. yoavmatchulsky

    yoavmatchulsky Member

    Messages:
    57
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #5
    function fetch_file(url)
    {
      var xmlhttp;
      if (window.XMLHttpRequest)
      {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
      }
      else if (window.ActiveXObject)
      {
      // code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      else
      {
        alert("Your browser does not support XMLHTTP!");
      }
    
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4)
       {
          var file_content = xmlhttp.responseText;
       }
     }
    xmlhttp.open("GET",url, true);
    xmlhttp.send(null);
    
    }
    
    Code (markup):
    i mashed it from w3school tutorial, so maybe its buggy, but the general solution is using ajax to fetch the file..
     
    yoavmatchulsky, May 14, 2009 IP
  6. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    You will not be able to use javascript to fetch a file from a remote server. The file that you are required to fetch must exist on the same server as the javascript that is trying to fetch it.
     
    camjohnson95, May 14, 2009 IP