简体   繁体   中英

Load text from a .txt file and display in jsPsychHtmlButtonResponse

I want to use jsPsychHtmlButtonResponse to display a .txt-file from a local folder on my computer. The reproducible code below does not work, while a very similar adaptation works for the display of images with jsPsychImageButtonResponse.

Does not work:

var text_display = {
  type: jsPsychHtmlButtonResponse,
  stimulus: text_files/test.txt,
  choices: ["Ready"],
};

Works:

var image_display = {
  type: jsPsychImageButtonResponse,
  stimulus: images_files/test.png,
  choices: ["Ready"],
};

Do you have any suggestions how to handle this problem?

Text files behave differently from image files in this context. While browsers can load an image file directly from a path, text files need to be loaded using more generic methods. This question has answers that demonstrate how text files are loaded. You could use fetch() to load in all the text files and assign them to variables that you then use in your experiment.

Alternatively, if you want something that is as close as possible to this experience without getting into fetch() calls, then you could put the text from your text file inside a JavaScript file and assign it to a variable.

var test_txt = `Contents from text file`;

Suppose that this has the filename test_txt.js . You can then load this in your HTML doc like you load other scripts (eg, jspsych.js).

<head>
    <script src="test_txt.js"></script>
</head>

Then you can use the test_txt variable in your code.

var text_display = {
  type: jsPsychHtmlButtonResponse,
  stimulus: test_txt,
  choices: ["Ready"],
};

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