Script API Tutorial

Redirect Respondent to the another Survey

In this example we will demonstrate you how to redirect respondents to a certain web-site for some purpose and then accept his/her return to the current survey. We will create two surveys in CoolTool. The first survey will just welcome a respondent and let him know that he will be redirected to another Survey. In the second survey we will ask the name of Respondent and then send him back to the first Survey. In addition we will pipe the given answer from the second Survey and show it in the last question of the first Survey.

Note, in the current example Surveys run under the same domain (cooltool.com) but you can redirect Respondent to any other website.

Try how it works below

In order to reproduce this example please undertake the following steps. Create the first survey which will contain three questions of the following types: "Instruction text", "External Redirect" and one more "Instruction Text". Write any welcome message in the first "Instruction Text" question.

Thereafter create the second survey with "OpenEnded" question. Use "Public link" data collector for this survey and copy its link. Now come back to the first Survey and choose External Redirect question. Paste the copied link into the Question name and add the following string "?from=@backlink@" to the link.

After that your “External Redirect” question should look like this pattern, [LINK_TO_THE_COLLECTOR]?from=@backlink@ where the [LINK_TO_THE_COLLECTOR] is the web link to another Survey. For example, for this tutorial the "External Redirect" question is "http://cooltool.com/psrvd4ekrqs?from=@backlink@"

Pay a little bit attention to the @backlink@ word in this parameter. When the Respondent is redirected to another web site this part will be automatically substituted by the return link. In the second Survey we will use it to send the Respondent back. Now, go the second Survey, and insert the following script into “Item setup script” tab of the "OpenEnded" question:

  • //return the parameters from the URL request (the part of URL that goes after "?")
  • var location = window.location.search;
  • var backlink = null;
  • //function that extracts @backlink@ from the incoming request
  • function getParameterFromHref() {
  • var params = location.split("from=");
  • var length = params.length;
  • //decode the incoming link
  • backlink = decodeURIComponent(params[1]);
  • }
  • //invoke getParameterFromHref function
  • getParameterFromHref();
  • //store the decoded link to the “window.backlink” variable
  • window.backlink = backlink;

This script accepts the incoming request, extracts and saves the return link for future use. Now we will demonstrate how to use extracted link and send the respondent back to the first survey.

Go to "OnNextPage script" tab and insert the following script:

  • //get Respondent answer
  • var answer = this.get()[0].value;
  • //add Respondent answer to the return link as parameter
  • var new_location = window.backlink+'?answer='+answer;
  • //redirect Respondent to the first Survey
  • window.location = new_location;

Now we need to accept Respondent’s answer in our first survey and show it in the last Question. Return to the first survey, choose the second “Instruction Text” question and insert the following script into “ItemSetup script” tab:

  • var location = window.location.search;
  • var answer = null;
  • getParameterFromHref();
  • function getParameterFromHref() {
  • var params = location.split("answer=");
  • var length = params.length;
  • //backlink = decodeURIComponent(params[1]);
  • answer = params[1];
  • }
  • window.answer = answer;

You should be already familiar with this script. Here we’re extracting the answer received in the second Survey and store it into "window.answer" variable.

The last thing left to do is to pipe the received answer from the second survey and show it in the last question. Add this string "{{window.answer}}" to the text of that last question.