user2463517
user2463517

Reputation: 197

How to read local textfile using html/javascript?

I am trying the following to write the contents of a local text file (version.txt) to the screen, but it isn't working.

<!DOCTYPE html>
<html>

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
        <script charset="utf-8" src = "jquery-1.10.1.min.js"></script>
        <script charset="utf-8" src = "cordova-2.7.0.js"></script>



<script>
function test()
{
    $.get("version.txt", function(data) {
     document.write(data);
    });
}
</script>

<body>
<button type="button" onclick="test()">Print version.txt</button>
</body>
</html>

Any idea what I'm doing wrong?

update: this works, I was just mistyping the name of the jquery file I was including.

Upvotes: 2

Views: 8279

Answers (2)

GJK
GJK

Reputation: 37389

  1. You're missing a closing brace. The console will clearly tell you about invalid Javascript if you look.
  2. You can't use document.write after the page has loaded. (Well you can, but you won't get the result you want.) Use jQuery append() instead.

Upvotes: 2

Rakesh Shetty
Rakesh Shetty

Reputation: 4578

I don't know wheater it is typo or not you are missing closing brace for function test

<script>
function test()
{
    $.get("version.txt", function(data) {
     document.write(data);
    });

}
</script>

Upvotes: 0

Related Questions