You need to check for status 0 (as when loading files locally with XMLHttpRequest
, you don’t get a status returned because it’s not from a Webserver
)
function readTextFile(file) { var rawFile = new XMLHttpRequest(); rawFile.open("GET", file, false); rawFile.onreadystatechange = function () { if(rawFile.readyState === 4) { if(rawFile.status === 200 || rawFile.status == 0) { var allText = rawFile.responseText; alert(allText); } } } rawFile.send(null); }
And specify file://
in your filename:
readTextFile("file:///C:/your/path/to/file.txt");
Related Posts:
- How to read a local text file?
- Pure JavaScript Send POST Data Without a Form
- Send POST data using XMLHttpRequest
- How can I upload files asynchronously with jQuery?
- XMLHttpRequest module not defined/found
- XMLHttpRequest status 0 (responseText is empty)
- Origin null is not allowed by Access-Control-Allow-Origin
- How to enable CORS in AngularJs
- How to read and write into file using JavaScript?
- Check if a string has a certain piece of text [duplicate]
- How to convert a string to an integer in JavaScript?
- How do I make the first letter of a string uppercase in JavaScript?
- window.location.href and window.open () methods in JavaScript
- Define a global variable in a JavaScript function
- Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’
- Cross-Origin Read Blocking (CORB)
- How to create an associative array in JavaScript literal notation
- Include another HTML file in a HTML file
- Cannot open local file – Chrome: Not allowed to load local resource
- How can I refresh a page with jQuery?
- JavaScript null check
- Remove Object from Array using JavaScript
- webpack: Module not found: Error: Can’t resolve (with relative path)
- Remove duplicate values from JS array
- Add days to JavaScript Date
- ajax jquery simple get request
- How to resolve TypeError: Cannot convert undefined or null to object
- What does href expression do?
- Give a value to an ng-model=”searchText” input based on list item clicked in Angular JS
- Difference between textContent vs innerText
- How can I guarantee that my enums definition doesn’t change in JavaScript?
- How to write an inline IF statement in JavaScript?
- How to check if two arrays are equal with JavaScript?
- What is causing the error `string.split is not a function`?
- Does JavaScript have a built in stringbuilder class?
- Uncaught TypeError: Cannot read property ‘top’ of undefined
- Pass react component as props
- How to fix Cannot find module ‘typescript’ in Angular 4?
- using jQuery $(this).addClass not working, simple code not working
- onClick to get the ID of the clicked button
- HTML text input allow only numeric input
- How to change the buttons text using javascript
- jQuery: Uncaught Error: Syntax error, unrecognized expression
- What is the difference between String.slice and String.substring?
- How to use in jQuery :not and hasClass() to get a specific element without a class
- Crop the image using JavaScript
- What is the best way to remove a table row with jQuery?
- Adjust width and height of iframe to fit with content in it
- document.getElementbyId() returning null
- Seeding the random number generator in Javascript
- Bootstrap popover is not working
- Sort array of objects by single key with date value
- How to add a tooltip to an svg graphic?
- Only on Firefox “Loading failed for the script with source”
- How do you use a variable in a regular expression?
- How to embed a youtube playlist with a sidebar
- How to add a button dynamically using jquery
- External JavaScript Not Running, does not write to document
- Create a simple 10 second countdown
- Generate a Hash from string in Javascript
- jQuery if Element has an ID?
- Get name of object or class
- Complex JSON nesting of objects and arrays
- jQuery – Increase the value of a counter when a button is clicked
- Insert a string at a specific index
- What does `node –harmony` do?
- AngularJS POST Fails: Response for preflight has invalid HTTP status code 404
- Getting “Cannot call a class as a function” in my React Project
- Separating html and JavaScript in Flask [duplicate]
- How to use aria-expanded=”true” to change a css property
- Nonce retrieved from the REST API is invalid and different from nonce generated in wp_localize_script
- wp.customize.bind ready event not fired
- What does wp-list.js do?
- Preventing YouTube embeds loading multiple instances of player JS?
- How to combine javascript files without plugins?
- WordPress Errors? Update Failed! data.min.js
- How to handle malformed response from WP REST API?
- Use useSelect/useDispatch instead of withSelect/withDispatch
- Load script after block is inserted
- Trouble adding JavaScript in visual editor (Sharpspring embed code)
- How to set translations in javascripts for my plugin?
- How to make wordpress URLS google friendly for ajax driven sites?
- How can I use CKEditor with WordPress and let clients edit the content?
- Google PageSpeed Enable Compression isn’t working?
- How can I add Javascript in the header of all post pages and only post pages
- wp_enqueue_script not loading my custom js file
- Gutenberg getBlockIndex in save() function
- Widgets and Post/Page edit/new blank, console errors only, no server errors
- Custom JS text area in customizer is being formatted wrong in document
- Add captcha validation to my custom form for Frontend posting
- AddThis Sharing Bar Not Displaying on Custom Page Template – JS conflict?
- Add JavaScript-Library “Fitty” to WordPress
- Access checks with custom REST endpoints and backbone
- How to pass data to javascript in custom widget class
- How to use value from modal tinymce windowManager?
- early enqueueing javascript file in page template, not in functions.php
- Wysiwyg editor not working on 4.2.2
- jQuery + more won’t load in header
- How to add additional JavaScript code [duplicate]
- JavaScript errors