Reordering Survey Pages with Script

Scripting can be used to change the order of survey pages based on the answer to a question.
Reference Guide
Before pages can be reordered in script, they must first be randomised. Randomised survey pages can then be identified during the scripting process. Block Codes will need to be attached to the randomised pages that need to be reordered.

For information on randomising and applying Block Codes to survey pages, please use this link: Randomising the Order of Survey Pages

The following guide will demonstrate step by step, reordering 4 randomised survey pages named, A,B,C and D - based on a group selected in a previous choice question. For example, if Group 2 is selected in the choice question - the page order will be displayed as D,C,B,A through script. Each randomised page will be using a Block Code named TUTORIAL.

Try our sample here: Reorder Randomised Pages

Use the following steps to setup your survey:
  • 1. Add the pages to the survey, ensure that randomisation and Block Codes are applied to the pages that need to be reordered.
    Page Reorder Add Pages
  • 2. Add a choice question BEFORE the randomised pages. This choice question will determine the order of pages to be displayed based on the group selected. Ensure the question has a valid Question Access Code.
    Page Reorder Choice Question
  • 3. Add a script to the page directly BEFORE the randomised pages. Ensure the "Randomization Parameters are Tested prior to Next Button" scripting event is selected from the dropdown. The script to reorder pages will be executed only when the next button is clicked.
    Page Reorder Scripting Event

Setting up the Script


We highly recommended to use the Chrome Developer Console when preparing your script for testing purposes.

There are two key WSC methods used when reordering randomised pages:
  • wscScripting.getPageRandomizationItems()
  • wscScripting.setPageRandomizationItems()
The getPageRandomizationItems() method will be the starting point for acquiring an array of pages in the survey:

Page Reorder List
Script can then be used to reorder the pages in the array and set as the new order using the setPageRandomizationItems() method:
///Get the group question
var oQGroup = wscScripting.getQuestionByDataPipingCode('GROUP');

///Get the value of the choice selected at the group question
var oQGroupNumber = wscScripting.getFirstSelectedChoiceValue(oQGroup);

///Get all the survey pages
var aPages = wscScripting.getPageRandomizationItems();

///Setup an array to hold only the random survey pages
var aRandomPages = [];

///Setup an array that will hold the new page order
var aNewOrder = [];

///Loop through all the survey pages and PULL out ONLY the pages that are random and belong to the DAY1 block code and push into random pages array
for (i = 0; i < aPages.length; i++) {
if (aPages[i].blockCode == 'TUTORIAL') {
aRandomPages.push(aPages[i]);
  };
};

///If group 2 was selected, begin reordering the pages, add them to the new order array
if (oQGroupNumber == 2) {
  for (i = 0; i < aRandomPages.length; i++) {
    if (aRandomPages[i].title == 'D') {
  aNewOrder.push(aRandomPages[i]);
  };
};
for (i = 0; i < aRandomPages.length; i++) {
  if (aRandomPages[i].title == 'C') {
    aNewOrder.push(aRandomPages[i]);
    };
};
for (i = 0; i < aRandomPages.length; i++) {
if (aRandomPages[i].title == 'B') {
aNewOrder.push(aRandomPages[i]);
  };
};
for (i = 0; i < aRandomPages.length; i++) {
if (aRandomPages[i].title == 'A') {
aNewOrder.push(aRandomPages[i]);
  };
};

///Get the ordinal positions of the pages that need to be reordered
var aOrdinal = [];
for (i = 0; i < aPages.length; i++) {
if (aPages[i].blockCode == 'TUTORIAL') {
aOrdinal.push(i);
  };
};

///Reorder the pages
aPages[aOrdinal[0]] = aNewOrder[0];
aPages[aOrdinal[1]] = aNewOrder[1];
aPages[aOrdinal[2]] = aNewOrder[2];
aPages[aOrdinal[3]] = aNewOrder[3];

///Set the page order
wscScripting.setPageRandomizationItems(aPages);
};