简体   繁体   English

从 .txt 文件加载文本并在 jsPsychHtmlButtonResponse 中显示

[英]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.我想使用 jsPsychHtmlButtonResponse 从我的计算机上的本地文件夹中显示一个 .txt 文件。 The reproducible code below does not work, while a very similar adaptation works for the display of images with jsPsychImageButtonResponse.下面的可重现代码不起作用,而非常相似的改编适用于使用 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.您可以使用fetch()加载所有文本文件并将它们分配给您随后在实验中使用的变量。

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.或者,如果您想要在不进入fetch()调用的情况下尽可能接近这种体验的东西,那么您可以将文本文件中的文本放入 JavaScript 文件中并将其分配给变量。

var test_txt = `Contents from text file`;

Suppose that this has the filename test_txt.js .假设这有文件名test_txt.js You can then load this in your HTML doc like you load other scripts (eg, jspsych.js).然后,您可以像加载其他脚本(例如,jspsych.js)一样将其加载到您的 HTML 文档中。

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

Then you can use the test_txt variable in your code.然后您可以在代码中使用test_txt变量。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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