0

Currently im using this code to view content of a csv in a webbrowser with javascript. But this only works in firefox as other browser dont allow you to grab content from an iframe. I need to convert my csv to a txt if i want to use this method, and would like to keep csv instead of txt.

Do any of you know how I can make this work without the Iframe part and txt part so I can use it in different browser? Thanks!

<iframe id="frmFile" src="content/assets/output.txt" onload="LoadFile();" style="display: none;"></iframe>

<script>function LoadFile() {
var oFrame = document.getElementById("frmFile");
var strRawContents = oFrame.contentWindow.document.body.childNodes[0].innerHTML;

while (strRawContents.indexOf("\r") >= 0)
strRawContents = strRawContents.replace("\r", "");
var arrLines = strRawContents.split("\n");
var alltext = "";

if(arrLines[12].split(";")[8] == "0"){ }
for (var i = 1; i < arrLines.length; i++)
{
    alltext += "<tr>";
    var prop = arrLines[i].split(";");
    for (var j = 0; j < 11; j++) {
        alltext += "<td>" + prop[j] + "</td>";
    }
    alltext += "</tr>";
    document.close();
}
document.getElementById("message").innerHTML = alltext;
}
</script>

1 Answer 1

1

I would suggest Ajax get of the file -> that way you can get the text content.

<!-- no iframe -->

<script>
var url = "http://www.example.com/file.txt";
var txtFile = new XMLHttpRequest();
txtFile.open("GET",url,true);
txtFile.send();

txtFile.onreadystatechange = function(){
     if (txtFile.readyState== 4 && txtFile.status == 200)
     {              
         var strRawContents = jsonFile.responseText;
     }
}
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

Like this?: pastebin.com/raw/0xpCmgRE That gives this error: Uncaught ReferenceError: LoadFile is not defined at HTMLIFrameElement.onload
Edited the response. It's done completely without iframe and response text is the text of file (now named the same way as you have).
Using your code it says: Uncaught SyntaxError: Unexpected token var Using this (pastebin.com/raw/Zw2q8JtZ) code I get: MLHttpRequest cannot load file:///C:/Users/Jay/test/etc/output.txt. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
C'mon man at least sense check your code before posting. Half of the conditional statement is missing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.