https://github.com/benallfree/cordova-plugin-xhr-local-file
XHR polyfill to load file:// URLs
https://github.com/benallfree/cordova-plugin-xhr-local-file
Last synced: 3 months ago
JSON representation
XHR polyfill to load file:// URLs
- Host: GitHub
- URL: https://github.com/benallfree/cordova-plugin-xhr-local-file
- Owner: benallfree
- License: other
- Created: 2020-02-20T21:07:28.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-03-26T07:15:05.000Z (over 3 years ago)
- Last Synced: 2025-06-25T07:02:49.824Z (3 months ago)
- Language: JavaScript
- Size: 76.2 KB
- Stars: 5
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# cordova-plugin-xhr-local-file
This plugin is a polyfill for XmlHTTPRequest that will handle the `file://` protocol correctly using Cordova's [File](https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html) API behind the scenes.
## Installation
```
$ cordova plugin add cordova-plugin-xhr-local-file
$ cordova prepare
```## Usage
You may now make XHR requests to local files. Moreover, other JS libraries that load assets via XHR will now function normally even when loading
assets from your Cordova app's filesystem.Since Cordova apps are loaded from `file://`, every relative URL uses the `file://` protocol. This library resolves relative and absolute URLs using the Cordova `www` folder as the webroot.
```javascript
var oReq = new XMLHttpRequest()
oReq.open('GET', './foo.json') // If you are in ./www/index.html, this resolves to ./www/foo.json
oReq.send()
``````javascript
var oReq = new XMLHttpRequest()
oReq.open('GET', '/foo.json') // If you are in ./www/index.html, this ralso esolves to ./www/foo.json
oReq.send()
````cdvfile://` protocol is also honored:
```javascript
var oReq = new XMLHttpRequest()
oReq.open('GET', 'cdvfile://localhost/bundle/www/data/foo.json')
oReq.send()
```## Thanks
Thanks to [Oracle](https://github.com/oracle/cordova-plugin-wkwebview-file-xhr) for their XHR polyfill implementation, upon which this one is based.