JavaScript file
Read entire file into an array
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 | function file (url) { // Read entire file into an array // // version: 1008.1718 // discuss at: http://phpjs.org/functions/file // + 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');} |
Examples
Running
1 | file('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm'); |
Should return
1 | {0: '123'} |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have file goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>