Welcome to the LimeSurvey Community Forum

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

Registering tab switches

  • Nat1111
  • Nat1111's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 1 month ago #219736 by Nat1111
Registering tab switches was created by Nat1111
Hey all!,

I'm running a study online and would like to know when participants in the study switch tabs (e.g., as a rough, indirect proxy of attention). I've done this before on Qualtrics and was trying to adapt the logic, but I haven't succeeded thus far. I have:
  • Disabled the XSS filter.
  • Added the following code to custom.js (thinking this will be ran in every page so it will create a string with the question IDs where the tabswitching took place):
    Code:
    $(document).on('ready pjax:scriptcomplete',function(){
     
      // When the window is blurred (i.e., loses focus)
      $(window).blur(function(){
        if (tabSwitches !== undefined) { // We check if we have defined TB before
          var tabSwitches = ""; // If not, we define it
        }
        tabSwitches += ("{SGQ}"); // Then we add the question ID to the string
        tabSwitches += "&"; // And add & as a separator
      });
    });
  • Created a short free text question after all the questions in which I want tab switching to be registered. It then has the following code within the question field:
    Code:
    <script type="text/javascript" charset="utf-8">
    $(document).on('ready pjax:complete',function() {
    // Hide this answer field
    document.getElementById("answer{SGQ}").style.display='none';
    // Save tabSwitches
    $('input#answer{SGQ}').text(tabSwitches);
    // Troubleshoot
    alert("SGQ is: {SGQ} and tabSwitches is: " + tabSwitches);
    });
    </script>
As is, the alert isn't running at all (haven't checked the database, but I bet it's empty as well). I think it's because tabSwitches is undefined and spoils the rest (if I remove the tabSwitches variable, the alert runs correctly and replaces {SGQ} with the correct number), so I would love if anyone could help me out, particularly in understanding if it is possible to get custom.js to run in every page or whether I have to turn what I have there into a function that I call in every single question. Thanks!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 1 month ago #219738 by tpartner
Replied by tpartner on topic Registering tab switches
I think it's because you only define tabSwitches in a blur event which won't have happened as the page loads.

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: DenisChenu
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose