Use Iframe to embed csv and allow user to search f

2019-06-09 17:34发布

问题:

I have a simple html/jscript code here which allow users to enter a keyword and return result from sample csv data (simulated as var CSV in this code) to table format in HTML, now I wish to use the iframe where the CSV is embedded instead of referring my DB to this simulated CSV, how can I do that? This code is working fine but I need to refer my search database to iframe. Thank you in advance.

<html>
<head>
    <title>CSV Database</title>
    <script type="text/javascript">
        /* Following is a SIMULATED CSV file only.*/
        var CSV =  'APRIL,RED,HAPPY,TEACHER,092236015\n';
        CSV += 'BRIAN,YELLOW,SAD,ENGINEER,092236015\n';
        CSV += 'JIMBOY,BLUE,CRAZY,PROGRAMMER,092236015\n';
        CSV += 'JAYCEE,GREEN,MOODY,MAKE-UP_ARTIST,092236015\n';
        CSV += 'GALLIE,BLACK,SERIOUS,CYCLIST,092236015\n';
    </script>

</head>
<body onload="MakeDB()">
    Keyword: <input type="text" value="" id="ERP">
    <input type="button" value="Search"   onclick="CSVsearch(document.getElementById('ERP').value)">
    <p>
    <div id="tblDisplay"></div>
    <script type="text/javascript">

        var DB = new Array();
        function MakeDB() { DB = CSV.split('\n'); }

        function CSVsearch(dbInfo) {
            var posn = -1;
            for (i=0; i<DB.length; i++) {
                tmp = DB[i];
                if (tmp.indexOf(dbInfo) != -1) { posn = i; break; } 
            }
            if (posn == -1) { alert('No matching result from the file'); }
            else { document.getElementById('tblDisplay').innerHTML =    displayAsTable(DB[posn]); }
        }

        function displayAsTable(info) {
            var str = '<table border="1" width="35%">';
            var ary = info.split(',');
            str += '<tr><th>KEYWORD</th><th>INFO1</th><th>INFO2</th><th>INFO3</th><th>INFO4</th></tr>';
            str += '<tr><td>'+ary.join('</td><td>')+'</td></tr>';
            str += '</table>';
            return str;
        }
    </script>
    <br>
    <br>
    <br>
    <br>
    <br>
    <iframe src="file:///C:/Users/april_ibanga/Desktop/data.txt" name="data" frameborder="1" scrolling="auto" width="700" height="300" align="bottom"></iframe> 
</body>
</html>

回答1:

From what I can tell, you can't load just anything in an iframe and scrape it. The Javascript normally would be frameVar.contentWindow, but this is null when I attempt it with the src being a txt or csv file.

Web applications usually handle this via web service calls that return html,xml,json responses that represent the serialized file contents.

Good luck!