---
layout: page
title: "JavaScript file function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/file:398
- /functions/view/file
- /functions/view/398
- /functions/file:398
- /functions/398
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's file

{% codeblock filesystem/file.js lang:js https://raw.github.com/kvz/phpjs/master/functions/filesystem/file.js raw on github %}
function file (url) {
  // http://kevin.vanzonneveld.net
  // +   original by: Legaev Andrey
  // +      input by: Jani Hartikainen
  // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // %        note 1: This function uses XmlHttpRequest and cannot retrieve resource from different domain.
  // %        note 1: Synchronous so may lock up browser, mainly here for study purposes.
  // %        note 1: To avoid browser blocking issues's consider using jQuery's: $('#divId').load('http://url') instead.
  // *     example 1: file('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm');
  // *     returns 1: {0: '123'}
  var req = this.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
  if (!req) {
    throw new Error('XMLHttpRequest not supported');
  }

  req.open("GET", url, false);
  req.send(null);

  return req.responseText.split('\n');
}
{% endcodeblock %}

 - [view on github](https://github.com/kvz/phpjs/blob/master/functions/filesystem/file.js)
 - [edit on github](https://github.com/kvz/phpjs/edit/master/functions/filesystem/file.js)

### Example 1
This code
{% codeblock lang:js example %}
file('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm');
{% endcodeblock %}

Should return
{% codeblock lang:js returns %}
{0: '123'}
{% endcodeblock %}


### Other PHP functions in the filesystem extension
{% render_partial _includes/custom/filesystem.html %}
