1

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?

CapsLock
  • 11
  • 3

1 Answers1

1

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 (e.g., 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"],
};
Josh
  • 2,105
  • 1
  • 14
  • 14
  • Thank you very much, Josh! I really appreciate your help and all the effort you put in developing & maintaining jspsych as well as helping people like myself getting into programming cognitive science experiments! – CapsLock Jul 13 '22 at 15:04
  • The easiest way was actually using your approach from the tutorial on csv-json-transformation. Instead of storing multiple txt-files, I created a table containing all strings stored in one column and converted it to json-format! – CapsLock Jul 15 '22 at 13:01