====== Record browser tab switch ======

This page describes how you can record if the participant leaves the questionnaire’s browser tab – for example, if he opens a new tab to google the answer to a knowledge question.

**Important:** This function records every leaving of the browser tab –– not only the change of a tab but also a switch to a chat program, to the email-client etc.

**Important:** This function uses [[:de:glossary#javascript|JavaScript]]. If the variable does not contain any value, the participant had deactivated JavaScript.

**Advice:** The code is being executed in the particpant’s browser. With the necessary technical knowledge the user can manipulate the value.

**Tip:** Another valuable information about whether the participant has done something else while answering your questionnaire may be the dwell time of a page that can be downloaded with the dataset. 

===== Instructions =====

    - You need an internal variable that stores the output of the function. Create a question of the type //internal Variable// and create one item in it. If you name this item „tab change“ it will have a useful variable label in your output dataset.
    - Put this question on the page that you would like to measure (**Compose questionnaire**). This will create a hidden input field that can store the value the JavaScript code creates (see also [[:en:create:inputs-custom]]).
    - Create a text item [[en:create:texts]] containing the code below. Drag-and-drop this text item below the internal variable question. You can also drag a //html-code-element// below the question and enter the code there.
    - In the line ''%%blurCount("IV01_01")%%'' change ''IV01_01'' to the ID of your internal variable (item).

<code html>
<script type="text/javascript">
<!--

function blurCount(inputID) {
  var input = document.getElementById(inputID);
  if (!input) {
    alert("Error: the internal variable" + inputID + " is not available on this page!“);
    return;
  }

  // Initalize (value: 0)
  if (isNaN(parseInt(input.value))) {
    input.value = 0;
  }

  // Call this function when the window is being left
  function countBlur(evt) {
    var val = parseInt(input.value);
    input.value = val + 1;
  }

  // activate counter for every leaving of the page
  SoSciTools.attachEvent(window, "blur", countBlur);
}

// Activate counter
blurCount("IV01_01");

// -->
</script>
</code>

**Tip:** When creating your questionnaire, you can change the question type of the internal variable to an //open text input// to see the counter live. Set it back to an internal variable when starting the field phase of your survey.

**Advice:** This measurement is valid for one single page only — the one that contains the internal variable and the code. If you would like to observe more than one pages, create additional internal variables and add them together with this code on all pages (make sure to change the variable in the code each time).