Welcome to the LimeSurvey Community Forum

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

Using a parameter from the URL in script, but dissapears when changing language.

  • larslensink
  • larslensink's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 3 months ago - 3 years 3 months ago #210802 by larslensink
Hello,

I'm trying to have two different versions of the welcome text of my survey depending on the value of a parameter that is in the URL. I'm using Javascript to read the value of the parameter and with an if-statement it determines what version of the text to display. The parameter is also used to determine the value of question A5 on the next page. However, when I test this and change the language on the first page, the parameter disappears from the URL and the Javascript doesn't function properly anymore. The page shows the text that is under the 'else' condition in my javascript, no matter the parameter that was initially provided. It is almost like it forgot that a parameter was given, but the weird thing is that if I continue with the survey, the value for question A5 that was given is actually saved and shows correctly on the next page. This is the javascript I use:
Code:
<script type="text/javascript">
  function getParameter( parameterName ) {
    let parameters = new URLSearchParams(window.location.search);
    return parameters.get(parameterName);
  }
  var a = getParameter("A5");
    if( a == "A1") {
    document.write("version 1")
  }
  else {
    document.write("version 2")
  }
</script>

I have tried solving this in a number of ways, but nothing seemed to work. For example, I have tried adding a variable 'b' that I would try to assign the value of question A5 to using either
Code:
  var b = "{ A5.NAOK }";
or
Code:
  var b = "{ INSERTANS:SGQA }";

and then extending the if-statement to
Code:
    if( a == "A1" || b == "A1" ) {



I have tested this with setting the variable 'b' to "A1" manually and then it works, but I can't seem to get that value "A1" from either the URL or from question A5 after I have selected another language. I would be delighted if anyone could help me with this one!

Thanks in advance!

Lars
Last edit: 3 years 3 months ago by larslensink. Reason: I saw that bold and italics didn't work in the [code] section
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #210844 by tpartner
You could load the variable into local storage :

Code:
<script type="text/javascript" data-author="Tony Partner">
    function getParameter( parameterName ) {
        let parameters = new URLSearchParams(window.location.search);
        return parameters.get(parameterName);
    }
    
    var a;
    if (localStorage.getItem('itemA{SID}') != null) {
        a = localStorage.getItem('itemA{SID}');
    }
    else if(getParameter("Q1") != null) {
        a = getParameter("Q1");
        localStorage.setItem('itemA{SID}', a);
    }
    
    if( a == "A1") {
        document.write("version 1")
    }
    else {
        document.write("version 2")
    }
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: larslensink
The topic has been locked.
  • larslensink
  • larslensink's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 3 months ago #210845 by larslensink
Hi Tony,

Thanks so much! We have tried something with the local storage too, but that didn't work for us. This, however, does work! My thesis buddy and I are very thankful, haha :)

Cheers!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #210847 by tpartner
You're welcome.

Also, if you want to delete the local storage item at the end of the survey:
 
 
Code:
<script type="text/javascript" data-author="Tony Partner">
    localStorage.removeItem('itemA{SID}');
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose