javascript - 从 JavaScript 中的文本文件获取值

标签 javascript html variables get

我有一个 javascript 变量,它显示在我的 html 网站上,我还有一个 .txt 文件,它从 .php 脚本获取付款值。这些值在 .txt 文件中显示为 12345,每个金额加 1,因此 .txt 文件首先获取 1,然后 2,然后 3 等。现在我的问题是,如何从 .t​​xt 获取最后一个值文件并将我的变量值转换为我得到的值,通过 .txt 文件的最后一个值我的意思是如果文件中的当前值是 12345 那么最后一个值是 5,那么我想将 Javascript 中的变量更改为该值 5。

我的 javascript/html 脚本,其中包含变量并显示。

<div class="newValue">
        Current value: $<span id="newValueVariable"></span>
</div>

 <script type="text/javascript"> 

        var newValueVariable= 1;
        document.getElementById("newValueVariable").innerHTML = newValueVariable;

</script>

最佳答案

使用 AJAX:

var xhr = new XMLHttpRequest;
xhr.open("filename.txt", "GET", true);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status == 200) {
        var contents = xhr.responseString;
        // Replace next line with what you actually use to parse the file
        var lastChar = contents.substr(-1, 1);
        document.getElementById("newValueVariable").innerHTML = lastChar;
    }
}
xhr.send();

关于javascript - 从 JavaScript 中的文本文件获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29441097/

相关文章:

html - 单击单选按钮标签而不选择按钮本身

javascript - 在 Google 文档中添加文本和删除文本(插件)

javascript - 想要在网页上显示的 .obj 文件上有单击事件

session - 在 Lift Scala 中存储 session 变量

javascript - JavaScript 中 onclick 属性的变量范围

javascript - Jquery ajax 函数停止工作

javascript - Koa - 发出内部请求

javascript - 如何使用Symfony2中的href路径将javascript变量值从twig传递到 Controller ?

javascript - Redux Thunk函数后的回调: Uncaught TypeError: Cannot read property 'then' of undefined

javascript - 在 joomla 中使用什么更好,模块或条件语句来检查页面