解析文本文件到一个数组? [英] Parse a text file into an array?

查看:249
本文介绍了解析文本文件到一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有3个文件;他们是在同一目录下我的电脑上:

I currently have 3 files; they are in the same directory on my computer:


  • project.html

  • javascript.js

  • 的text.txt

code在project.html

<html>
<head>
    <link href="style.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="javascript.js">
    </script>
    <script
        //calling the Google Maps API
    </script>

    <script>
        //code to initialize Google Maps
    </script>
</head>

<body>
    <div class="content">
        <div id="googleMap"></div>
        <div id="right_pane_results">hi</div>
        <div id="bottom_pane_options">
            <button onclick="get_parameters()">Try It</button>
        </div>
    </div>
</body>
</html>

code在javascript.js

function get_parameters() {
    alert('hi');
    var xhr = new XMLHttpRequest();

    xhr.addEventListener('readystatechange', (output) => {
      if (xhr.readyState === 4) {
        var response = xhr.responseText;
        callback(response);
      }
    });

    xhr.open('GET', 'text.txt', true);
    xhr.send();
}

文字在'的text.txt'





等。(近150线)

目前一切的结束,我想分析的text.txt'到一个数组,并使用数组创建数组的内容的一个选项菜单。我已经发布了一个问题,询问我是否可以使用JavaScript解析文件和答案是否定的,由于安全问题。 JavaScript的code是什么,我都试过,但效果不好。这就是说,有没有我可以用它来读取该文件并创建选项菜单?另一种语言

At the end of everything, I would like to parse 'text.txt' into an array and use that array to create an option menu with the array's content. I have posted a question asking whether I can parse the file using JavaScript and the answer was no due to security issues. The JavaScript code is what I have tried but has not worked. That said, is there another language I can use to read the file and create the option menu?

我最终会想有在'project.html'文件像这样(下图):

I would eventually like to have something like this (below) in the 'project.html' file:

<select>
    <option value="1">Line 1 of File</option>
    <option value="2">Line 2 of File</option>
    <option value="3">Line 3 of File</option>
        <!-- etc... -->
</select>

有些事情要记住:

Some things to keep in mind:


  • 所有文件的不是在服务器上但而从我的计算机上运行(所以我猜服务器尺寸的语言,如 PHP AREN 'T选项)

  • 我想根据的text.txt'
  • 内容自动创建一个选项菜单
  • 的文件是在同一目录

  • 我不希望用户能够选择阅读的文件。 '的text.txt']是从制成并填充由我已经作出,并有可能改变另一个程序中的文件;这就是为什么我希望每一次读取文件加载页面。

  • All files are not on a server but are rather running from my computer (so I'd guess server-size languages like PHP aren't an option)
  • I would like to auto-create an option menu based on the content in 'text.txt'
  • The files are in the same directory
  • I would not like the user to be able to choose which file to read. 'text.txt' is a file that is made from and filled by another program I have made and is likely to change; that's why I want to read the file every time the page loads.

推荐答案

如果你可以调整浏览器设置没有理由为什么你不能只用JavaScript的做到这一点。下面是使用jQuery一个例子,你可以把它转换成普通的JavaScript但会采取了很多code。

If you can tweak browser settings there is no reason why you can't do this with just javascript. Below is an example using jQuery, you could convert it to plain Javascript but that would take a lot more code.

$(document).ready(function() {
    //fetch text file
    $.get('text.txt', function(data) {
        //split on new lines
        var lines = data.split('\n');
        //create select
        var dropdown = $('<select>');
        //iterate over lines of file and create a option element
        for(var i=0;i<lines.length;i++) {
            //create option
            var el = $('<option value="'+i+'">'+lines[i]+'</option>');
            //append option to select
            $(dropdown).append(el);
        }
        //append select to page
        $('body').append(dropdown);
    });
});

它工作得很好IE10上只是一个对话框,询问我是否愿意被阻止的内容来运行。其他浏览器可能或maynot需要扮演其他安全设置,以获得它的工作。

It worked fine on IE10 with just a dialog asking if I wanted blocked content to run. Other browsers may or maynot need other security settings played with to get it to work.

这篇关于解析文本文件到一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆