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
3 months 2 weeks ago - 3 months 2 weeks 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: 3 months 2 weeks ago by holch.

Please Log in to join the conversation.

  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 months 2 weeks ago - 3 months 2 weeks 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" />

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.
Last edit: 3 months 2 weeks 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
3 months 2 weeks 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
3 months 2 weeks 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.

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.

Please Log in to join the conversation.

  • f.dragh
  • f.dragh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 months 2 weeks 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.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 months 1 week ago #267506 by Joffm
Replied by Joffm on topic JavaScript code in LimeSurvey
Hi,
when I enter an image with the wizard I get
 

and in the source code
Code:
<img alt="" src="/lime6/upload/surveys/365867/images/a2.PNG" />

You see the backslash at the start?

As said: Always better you send a lss export.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • f.dragh
  • f.dragh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 months 1 week ago #267507 by f.dragh
Replied by f.dragh on topic JavaScript code in LimeSurvey
Ok, thanks for the advice. 
I've attached the lsg file of the group of question where I have issues.

I hope this helps solve the issue.
Thanks again!

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 months 1 week ago - 3 months 1 week ago #267509 by Joffm
Replied by Joffm on topic JavaScript code in LimeSurvey
Please, we always ask for lss files, neither lsq nor lsg.
Copy the survey, remove everything not relevant and send the lss export.

And, what happened when you added the slash to the link - or even entered the ful path?

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 months 1 week ago by Joffm.

Please Log in to join the conversation.

  • f.dragh
  • f.dragh's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 months 1 week ago #267517 by f.dragh
Replied by f.dragh on topic JavaScript code in LimeSurvey
Ok, sorry for the misunderstanding, I'm attaching the LSS file.
Even when I try using a slash or something else in the path, nothing changes.Thanks again.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 months 1 week ago #267521 by Joffm
Replied by Joffm on topic JavaScript code in LimeSurvey
It can't work if you create the random number in the same group
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: f.dragh

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose