read txt file via client javascript - javascript

I'm new to javascript and trying to open a txt file into var and then inject it to html div...
I tried to use fopen but I didn't succeed.
<script type="text/javascript">
file = fopen(getScriptPath("info.txt"), 0);
file_length = flength(file);
var content = fread(file,file_length);
var div = document.getElementById("myDiv");
//alert(div);
div.innerHTML = "";
div.innerHTML = content;
</script>

Although it says xml request this works perfectly fine for txt files too (server and client side).
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","YOUR_FILE.txt",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseText;

JavaScript has none of the functions you are trying to use.
To read files on the server in JavaScript, you can use XMLHttpRequest.
There is no easy way to read files on the client machine.

abandoned question:
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","YOUR_FILE.txt",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseText;
by Freek8

For security reasons, Javascript is made so you can not do this. However, a person has made a workaround which may work and posted it here.
Ok, I realize, it only works for files that are publicly accessbile on the server, which I believe is not what you want to do. Still, if you do find a way, it will be a hack like this, but it could also be fixed to not work at any time.

Related

Calling second XMLHttpRequest after first one XMLHttpRequest (failed)

First one XMLHttpRequest is calling script to update MySQL database it can run multiple times depending on the situation. Script insertLocation will do the job, but in the end I am calling second XMLHttpRequest which should update file made from database. The problem is the second XMLHttpRequest will never read from updated database so there are data missing from first XMLHttpRequest.
However when I look to database, the data are there. I tried even sleeping before the creating of the file, but it did not helped. It seems like calling 2 XMLHttpRequest is not possible. I know about that third parameter of .open() , that when its set to false it will wait for request to be done, but it is now working. Here is my code:
insertToDatabase(lat,lng,id);
//there I tried 10 second sleep
updateDatabaseFile();
function updateDatabaseFile() {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","updateCoords.php",false);
xmlhttp.send();
alert("updated");
}
function insertToDatabase(lat,lng,id) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","insertLocation.php?lat="+lat+"&lng="+lng+"&id="+id,false);
xmlhttp.send();
alert("inserted")
}
One lats thing, when I run the updateDatabaseFile.php manually it will do the job. So I think the problem might be that changes done to database are made when this whole script ends so that is why I am getting incomplete data when reading.
EDIT: the alert("inserted") will open after the alert("updated") so there is the fail. The second one will finish first.

how to read xml document from a browse file using ajax and javascript?

I have this
var dname="download.xml";
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
//xmlhttp.overrideMimeType('text/xml');
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{//alert(xmlhttp.status);
if (xmlhttp.readyState==4 || xmlhttp.status==200)
{
Code.tabClick('xml');
//alert(xmlhttp.responseText);
//document.getElementById("content_xml").innerHTML="";
document.getElementById("content_xml").value=xmlhttp.responseText;
}
}
xmlhttp.open("GET",dname,true);
xmlhttp.send();
currently I have the static xml file to read the xml content.Now if I keep a option to browse file through this,
<input type='file' name='xmlfile'>
is it possible to read the content while the file is browsed?
I think these links will useful to you
From stackoverflow
Load xml file with javascript

Script (AJAX or JS) does not work properly in IE8

I have a js/ajax script at http://worldcitiesdatabase.info/test1/
I just received a complaint that it does not work properly in IE8.
Onchange seems to work, but then the next menu is not populated.
Can you please help me figure it out?
Thanks
Not sure what the problematic part of the code is. Here is my guess:
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
newList="";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
newList = xmlhttp.responseText;
m1.innerHTML=newList;
m1.disabled=false;
}
if (menuname=="showCountry") {
var c1=document.getElementById('showRegion');
if (c1.options.length==2) {
if (c1.options[0].value=='NONE') {
c1.remove(0);
c1.value='0';
reloadmenu(c1);
}
}
}
}
xmlhttp.open("GET",newFile+".php?q="+menuvalue,true);
xmlhttp.send();
The line
m1.innerHTML=newList;
is the culprit because you use innerHTML to add <option>s to <select>. This is a known bug in IE8 - http://support.microsoft.com/kb/276228.
Note: You can run your test page in your Internet Explorer if you just open Developer Tools and run it in IE8 mode (see for example http://techathlon.com/internet-explorer-10-run-compatibility-mode/)

Javascript is working on Safari, but fails on others

There is some problem that I can't find out why the code worked on Safari, but failed on other browsers.
There is the html part
and the main javascript part.
The main problem that I find is:
While executing the function downloadurl(url, function), cannot find the XML tags "Info", and the markers array length is 0 on many browsers. However, it's ok on Safari. The part of javascript code is like:
downloadUrl("http://travel-taoyuan.tycg.gov.tw/content/travel/xmlcontentlist1.aspx", function(doc) {
var xml = xmlParse(doc);
var markers = xml.documentElement.getElementsByTagName("Info");
......
To alert markers, it will return "0".
And actually it should be "174"(Safari's result).
Thanks for answering my question.
hmmm, seems to be a syntax error. Different browsers will use a different syntax, and so it won't execute (or at best, not properly if your lucky).
Try something along the lines of....
<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://travel-taoyuan.tycg.gov.tw/content/travel/xmlcontentlist1.aspx",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("theMainTagName"); //TODO: this is the main tag ^(see note)
for (i=0;i<x.length;i++)
{
//TODO: code to handle each xml element
//this is the code to get the value from a particular tag: x[i].getElementsByTagName("theTagName")[0].childNodes[0].nodeValue
}
</script>
NOTE: ^the 'main' tag is, for example, in the case of this xml document, the 'main' tag is 'CATALOG'
Hope this helps. Sorry if it's not very clear, been a while since I worked with xml/javascript. Comment if you want further explanation

Load html file into a div with javascript?

I have a div which I want to populate with different html files from my server. Is there a simple way to do this? All I ever find are jquery samples and I don't want to use a library.
I have tried this:
document.getElementById('main').innerHTML = 'menu.html';
But that obviously just loaded text!
Some simple Ajax will do the trick for you. This is untested, but should give you the right idea:
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("main").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","menu.html",true);
xmlhttp.send();
Simplest way is to use a library, which the entire point of, they wrote the code for u. right?
Use the jQuery example. It is as good as it gets.
Is there a simple way? No, not without a library. That being said, you can do it on your own if you choose - that might be all that's necessary for you to come crawling back into the arms of jQuery ;)
Resource: XHR on Mozilla Developer Network

Categories

Resources