Reputation: 1014
I want to know if there are any methods other than file reading api and XMLHttprequest()
to read/load text files which support almost all the web browsers.
I'm not using xmlhttprequest()
since it cannot be run in few browsers, like chrome(since it has a bug) you may find it here
I would like to know pure javascript/html methods for this and would like to avoid php and django scripts.
I tried following xmlhttprequest method
<!doctype html>
<html>
<head>
<body>
<script>
if (window.File && window.FileReader && window.FileList && winddow.Blob) { //alert('Great success! All the File APIs are supported.');
}
else {
alert('The File APIs are not fully supported in this browser.');
}
var req = new XMLHttpRequest();
req.open('GET', 'text.txt');
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
var lines = req.responseText.split(/\n/g);
lines.forEach(function(line, i) {
alert(line);
});
} else {
(alert("please update your browser,this webpage doesnt have support for your browser"))
}
}
}
req.send();
</script>
</body>
</head>
</html>
This runs in firefox 35.0.1
But when I try using it in chrome it doesn't work
But if I remove the condition if(req.status==200)
then I receive one alert which has no text.
Upvotes: 1
Views: 110
Reputation: 14645
The user-controlled file-api and scriptable XMLHTTPRequest are pretty much the only options. Alternatively, you might have luck with java or flash or silverlight (etc) approaches (I'll leave that up to other answers for now).
Alternatives for your own (development) machine:
For chrome you could allow XMLHttpRequest to access files from other files using:
--allow-file-access-from-files
That would be at least safer then using: --disable-web-security
Another quick solution would be "tiny": https://www.ritlabs.com/en/products/tinyweb/download.php
I like it because it is small!! Then (either via loop-back ip or local machine ip or host-name via hostfile) I work around those limitations, clearing the path for cookie/sandbox/local-storage features and the problem you are trying to solve.
However that doesn't help with a simple distributable html/javascript-app... As you pointed out, it is currently a "won't-fix" for chrome.
Upvotes: 1
Reputation: 1045
If you have access to the text file, you could edit it to be
var text = "ORIGINAL TEXT"
And add the file as a script tag.
Upvotes: 0