How to get var value from a text file?

Jenny

For example, there is a http://test.com/a.txt file.

['http://a.com', false],
['http://b.com', false],
['http://c.com', false],
['http://d', false]

I want to use this file as a var value like below:

<script>
...
var list = [ ***I want to get that .txt file as values here.*** ];
...
</script>

result

<script>
...
var list = [ 
['http://a.com', false],
['http://b.com', false],
['http://c.com', false],
['http://d', false]
];
...
</script>

How can I make it? I tried the javascript code below:

jQuery.get('http://test.com/a.txt', function(data) {
// The type o breaking line caracter will vary depending on OS
var values = data.split("\n");       
var List = values[values.length-1];              
});

But it didn't work...

T.J. Crowder

If you're doing this on a page with the same origin as that file (see the Same Origin Policy), or if that file's server supports cross-origin requests from your site, you could easily parse it as JSON by wrapping it in [ and ] and converting ' to ":

jQuery.get('http://test.com/a.txt', function(data) {
    var list = JSON.parse("[" + data.replace(/'/g, '"') + "]");
    // ...
});

Naturally this assumes there are no ' inside the '-quoted strings in the data.

If it's a cross-origin request that the server doesn't allow, you can't do it from a browser; you'd need to make the request to your own server and have your server request it from the other side.


Re your comment:

I just tried it but it didn't work. It shows an error message. Uncaught SyntaxError: Unexpected token ; in JSON at position 10233 at JSON.parse ()

Then the data isn't as you show in your question, because if it were, it would work:

var data =
  "['http://a.com', false],\n" +
  "['http://b.com', false],\n" +
  "['http://c.com', false],\n" +
  "['http://d', false]";
console.log("data:");
console.log(data);
console.log("parsed:");
var list = JSON.parse("[" + data.replace(/'/g, '"') + "]");
console.log(list);
.as-console-wrapper {
  max-height: 100% !important;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to get text value of selected text from onSelect DOM event

From Dev

bash how to get var from file with multiple lines

From Dev

Get value of parameter from text file using sed

From Dev

How get value from text file in linux

From Dev

How I can get a value from a text file and select a checkbox who have that value?

From Dev

How to get the value from rows and columns from text file in bash?

From Dev

how to send value var s from jquery to show in input tag id="text_var"?

From Dev

How to get VBS to read from text file?

From Dev

get value from all array var in a php file

From Dev

PHP: How to get a value from a JSON file?

From Dev

bash how to get var from file with multiple lines

From Dev

Using a var from a text file

From Dev

How to get a string from external PHP file and set it as var in JavaScript

From Dev

Get value of parameter from text file using sed

From Dev

How to Get a value from a text file

From Dev

How to write text containing $var to a bash file?

From Dev

How to get word from text file BASH

From Dev

How to get text value of input type=file for multiple files?

From Dev

How to Get Text from Online File .text

From Dev

How to get value from one column of a text file for values of another column

From Dev

Get value from script to var in mvc

From Dev

how to get dictionary elements from text file

From Dev

how get each value from json file

From Dev

How to get output of particular text from text file using batch?

From Dev

How to read a value from a text file and store that value into a temporary variable?

From Dev

How to get numeric value from text field?

From Dev

How to bind file_get_contents() return value in text field

From Dev

How to set var value from a datasource file?

From Dev

How to get the value from a "key = value" string from a file?

Related Related

  1. 1

    How to get text value of selected text from onSelect DOM event

  2. 2

    bash how to get var from file with multiple lines

  3. 3

    Get value of parameter from text file using sed

  4. 4

    How get value from text file in linux

  5. 5

    How I can get a value from a text file and select a checkbox who have that value?

  6. 6

    How to get the value from rows and columns from text file in bash?

  7. 7

    how to send value var s from jquery to show in input tag id="text_var"?

  8. 8

    How to get VBS to read from text file?

  9. 9

    get value from all array var in a php file

  10. 10

    PHP: How to get a value from a JSON file?

  11. 11

    bash how to get var from file with multiple lines

  12. 12

    Using a var from a text file

  13. 13

    How to get a string from external PHP file and set it as var in JavaScript

  14. 14

    Get value of parameter from text file using sed

  15. 15

    How to Get a value from a text file

  16. 16

    How to write text containing $var to a bash file?

  17. 17

    How to get word from text file BASH

  18. 18

    How to get text value of input type=file for multiple files?

  19. 19

    How to Get Text from Online File .text

  20. 20

    How to get value from one column of a text file for values of another column

  21. 21

    Get value from script to var in mvc

  22. 22

    how to get dictionary elements from text file

  23. 23

    how get each value from json file

  24. 24

    How to get output of particular text from text file using batch?

  25. 25

    How to read a value from a text file and store that value into a temporary variable?

  26. 26

    How to get numeric value from text field?

  27. 27

    How to bind file_get_contents() return value in text field

  28. 28

    How to set var value from a datasource file?

  29. 29

    How to get the value from a "key = value" string from a file?

HotTag

Archive