Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

JavaScript code in LimeSurvey

  • f.dragh
  • f.dragh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
1 day 4 hours ago - 1 day 1 hour ago #267478 by f.dragh
JavaScript code in LimeSurvey was created by f.dragh
==================
(Write here your question/remark)Please help us help you and fill where relevant:
LimeSurvey version: LimeSurvey Community Edition  Versione 6.6.2+240827 
Own server or LimeSurvey Cloud: Own server
Survey theme/template:
==================
Hello,

I need to create a question that randomly selects an image from a folder I created on the LimeSurvey server (containing 32 images). To do this, I created a "Text Display" type question and inserted a JavaScript code. Unfortunately, in the question preview, a blank question appears as if it can't read the code. At this point, I tried with a very basic test code, and I realized that my LimeSurvey can't execute JavaScript.

Here is my code:

<p><img id="randomImage" src="" alt="Immagine casuale" /></p>

<script type="text/javascript">
    var numberOfImages = 32;
    var randomIndex = Math.floor(Math.random() * numberOfImages) + 1;
    var imageFolderPath = 'https:folder_path';
    var imageFileName = 'Scenario_' + randomIndex + '.jpg'; //the images are named Scenario_1, Scenario_2, … , Scenario_32
    document.getElementById('randomImage').src = imageFolderPath + imageFileName;
</script>

In your opinion, is the problem in the code? Is there some setting related to JavaScript execution? If so, can you recommend a new method to achieve what I need?

I also tried this second method, but it doesn't seem to work either:
I created an "Equation" type question where I generate a random number from 1 to 32.
Then I created a "Text Display" type question where I inserted the following code:

<p><img alt="Random Image" src="Folder_path/Scenario_{G03Q03}.jpg" /></p>

Where G03Q03 is the Code of the question where I've generated the random number. I already checked the accessibility of the images, the correcteness of the folder path and the images are all saves as jpg.

Thanks in advance for your help.
Last edit: 1 day 1 hour ago by holch.

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 day 1 hour ago - 1 day 1 hour ago #267485 by holch
Replied by holch on topic JavaScript code in LimeSurvey

At this point, I tried with a very basic test code, and I realized that my LimeSurvey can't execute JavaScript.


You most probably have the XSS filter on and are not a Superadmin. Switch it off and Javascript will work. If you have no way to switch off the XSS filter, no problem.

What you are trying to do can be easily done (I think even easier than with Javascript) via Expression Manager/script.

Create a equation type question (which you will hide via GUI later on, when everything is tested and works).

Let's call this question RandomImage or something and you will generate a random number from 1-32 via expression script.

How to do this has been described on the forum a million times. Important is, that you not just create the random number, but check before if this number already exists and only if not, create the random number. This avoids that the random number changes throughout the survey for the same respondent. Otherwise, any further call to the equation (e.g. because you want to use RandomImage more than once, or because the respondent went back in the survey) will generate a new random number.
Code:
{if(is_empty(RandomImage),rand(1,32),RandomImage.NAOK)}

Then you can use your approach to display the image:
Code:
<img alt="Random Image" src="Folder_path/Scenario_{RandomImage}.jpg" />

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

Last edit: 1 day 1 hour ago by holch.
The following user(s) said Thank You: f.dragh

Please Log in to join the conversation.

  • f.dragh
  • f.dragh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
23 hours 46 minutes ago #267490 by f.dragh
Replied by f.dragh on topic JavaScript code in LimeSurvey
Thank you so much for your response!

However, I still haven't been able to solve the issue. I inserted the two codes as you suggested, but in the question preview, the image still isn't loading. I'll attach some screenshots to show you better. In the code I added also some parts in order to display the complete image path and the random number generated. It seems like it is all good, i don't understand.
 Thanks again!

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
23 hours 40 minutes ago #267492 by holch
Replied by holch on topic JavaScript code in LimeSurvey
so the URL generated is the correct one?

How does the generated URL within SRC look like?

Does the image exist under this URL? Seems like that is the problem, the URL doesn't point to an image.

Screenshots are only slightly helpful. A LSS file is always the better choice.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

Please Log in to join the conversation.

  • f.dragh
  • f.dragh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
23 hours 30 minutes ago #267494 by f.dragh
Replied by f.dragh on topic JavaScript code in LimeSurvey
Yes, the URL generated is correct and accessible. ( sia.unibg.it/limesurvey/upload/surveys/4...1-32/Scenario_31.jpg )

<img alt="Game 1" src="limesurvey/upload/surveys/438158/images/scenari1-32/Scenario_{RandomImage}.jpg" />

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose