简体   繁体   中英

Text to HTML with Javascript and Auto Refresh

I have a script that pulls info from a text file and displays it in html. The text I pull is in this format with line changes for each event.

2017-09-20 19:01:13 INFO Application - Starting Application 
2017-09-20 19:01:13 INFO Application - No active profile set, 
2017-09-20 19:01:22 INFO Application - Started Application in 10.177 seconds
2017-09-20 19:22:32 INFO Util - Detected configuration changes

When I run my script it turns out like this in my html page:

2017-09-20 19:01:13 INFO Application - Starting Application 2017-09-20 19:01:13 INFO Application - No active profile set, falling back to default profiles: default 2017-09-20 19:01:22 INFO Application - Started Application in 10.177 seconds (JVM running for 12.187) 2017-09-20 19:22:32 INFO Util - Detected configuration changes.

Anyone know if it can be displated exactly as its in the text file on the website? Thank tyou.

    <span id="botlog">Loading data...</span>
    <script>
    function radioTitle() {
    var url = 'demotext.log'; 

    $.ajax({
       type: 'GET',
       url: url,
       dataType: 'text',
       success: function(data) {

       $("#botlog").html(data)
    },

    error: function(e) {
       console.log(e.message);
    }
    });

    }

    $(document).ready(function(){

    setTimeout(function(){radioTitle();}, 5000);
    setInterval(function(){radioTitle();}, 5000);

    });

    </script>

您可以将响应放入<pre>标记中(保留行距和换行符),或者根据需要使用以下Regex表达式插入<br />标记:

replace(/\n/g, "<br />");

You could replace the linebreaks:

replace(/(\n|\r)/g,'<br />'));

See also: javascript replace line breaks AND special characters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM