| Both sides previous revisionPrevious revisionNext revision | Previous revision |
| en:create:javascript [13.02.2018 09:17] – [Fehler finden] admin | en:create:javascript [23.10.2026 14:44] (current) – external edit 127.0.0.1 |
|---|
| PHP code (e.g. for filters) will only be executed when the participant submits a page in the questionnaire to the server after clicking on the Next button, which creates a new page. | PHP code (e.g. for filters) will only be executed when the participant submits a page in the questionnaire to the server after clicking on the Next button, which creates a new page. |
| |
| [[:en:glossary#javascript|JavaScript]] is required if the questionnaire should respond immediately when the participant clicks on a button or to user input ([[:en:create:dynamic|Immediately Show Questions when a Certain Option is Selected]]). JavaScript is executed directly in the participant's browser (as long as JavaScript has not been disabled,) and can respond to user input instantly. However, the user cannot access (or only has limited access to) the current questionnaire (e.g. participant's previous responses) with JavaScript. | [[:en:glossary#javascript|JavaScript]] is required if the questionnaire should respond immediately when the participant clicks on a button or to user input ([[:en:create:dynamic|Immediately Show Questions when a Certain Option is Selected]]). JavaScript is executed directly in the participant's browser (unless JavaScript has been disabled) and can respond to user input instantly. However, JavaScript cannot access (or only has limited access to) the current interview (e.g. participant's previous responses) with JavaScript. |
| |
| The JavaScript library ''[[:en:create:soscitools|SoSciTools]]'' is available within the questionnaire. This provides useful functions that make it easier to work with questionnaire content. You can also use the PHP command ''[[:de:create:functions:library|library()]]'' to include standard libraries like jQuery, SCEditor or (if available on the server) highcharts. | The JavaScript library ''[[:en:create:soscitools|SoSciTools]]'' is available within the questionnaire. This provides useful functions that make it easier to work with questionnaire content. You can also use the PHP command ''[[:en:create:functions:library|library()]]'' to include standard libraries like jQuery, SCEditor or ChartJS. |
| |
| |
| **Note:** JavaScript cannot be mastered in an afternoon. Several examples of JavaScript are demonstrated in this manual which can be used directly in the questionnaire -- JavaScript can be extremely challenging when going beyond these examples. | **Note:** JavaScript cannot be mastered in an afternoon. Several examples of JavaScript are demonstrated in this manual which can be used directly in the questionnaire -- JavaScript can be extremely challenging when going beyond these examples. |
| |
| **Note:** Using JavaScript can harm the questionnaire's accessibility. People using a screenreader to fill in the questionnaire should keep in mind that the keyboard can be used to fill it in. | **Note:** Using JavaScript can harm the questionnaire's accessibility. People who fill in the questionnaire with a screen reader, for example, depend on being able to fill it in with the keyboard. |
| |
| **Tip:** If a JavaScript function does not work, the user has to rely on the browser's error console. Potential mistakes in the script can only be seen once the error console is opened. | **Tip:** If a JavaScript function does not work, the user has to rely on the [[:en:general:browser-tools|browser's error console]]. Potential mistakes in the script can only be seen once the error console is opened. |
| |
| **Tip:** If you want to program your own JavaScript, please ensure the questionnaire can still be filled in even if JavaScript has been disabled. | **Tip:** If you want to program your own JavaScript, please ensure the questionnaire can still be filled in even if JavaScript has been disabled. |
| ===== Inserting JavaScript ===== | ===== Inserting JavaScript ===== |
| |
| Create a new text element in **Text Elements and Labels** in order to use JavaScript on a page in the questionnaire. This element will contain HTML code and the actual JavaScript code between the ''<script>'' tags within this. | To use JavaScript on a page of the questionnaire, create a new text element |
| | * either in a section with **Add text** |
| | * or under **Text Elements and Labels**. |
| | |
| | Select the //Formatting// "HTML code" for the text. |
| | |
| | The HTML code can then contain the actual JavaScript code within a ''<script>'' tag. For example: |
| |
| <code javascript> | <code javascript> |
| **Tip:** In principle, HTML/JavaScript code can also be inserted directly into an HTML element of the questionnaire page. However, text elements offer several advantages, which is why inserting the code directly is not recommended. | **Tip:** In principle, HTML/JavaScript code can also be inserted directly into an HTML element of the questionnaire page. However, text elements offer several advantages, which is why inserting the code directly is not recommended. |
| |
| | |
| | ===== HTML and JavaScript ===== |
| | |
| | The HTML code of a page defines the content. HTML consists of so-called "tags", recognizable by the angle brackets. A tag has a name, and most opening tags (e.g. ''<div>'' opens a DIV tag) have a closing tag, recognizable by the slash (''</div>''). For more information, see [[:en:create:texts-html#the_most_important_html_tags|The Most Important HTML Tags]]. |
| | |
| | An HTML tag can contain attributes, i.e. named settings. The following ''<div>'' tag, for example, contains a ''style'' attribute for the display and an ''id'' attribute to assign it the HTML ID "myDIV": |
| | |
| | <code html> |
| | <div style="border: 1px solid red; padding: 20px;" id="myDIV">Content</div> |
| | </code> |
| | |
| | Such an ID is, so to speak, a name for the element. Using this name, JavaScript can address and modify the element with the function ''document.getElementById()''. |
| | |
| | <code javascript> |
| | <script> |
| | // Store the element in the JavaScript variable "block" |
| | var block = document.getElementById("myDIV"); |
| | |
| | // Hide the element after 2 seconds |
| | function hide() { |
| | block.style.display = "none"; |
| | } |
| | window.setTimeout(hide, 2000); |
| | </script> |
| | </code> |
| | |
| | **Note:** JavaScript code can only address HTML elements that already exist when the script is executed. In the example above, the ''<div>'' tag must therefore be placed above the ''<script>'' tag. You can, however, also use ''%%window.addEventListener("load", ...)%%'' so that JavaScript code is only executed once the page has been loaded completely. |
| | |
| | **Tip:** The elements that SoSci Survey uses to display questions and items usually have HTML IDs. The quickest way to find them is to open the right menu in your browser: [[:en:general:browser-tools]] |
| |
| |
| * [[:en:create:dynamic|Immediately Show Questions when a Certain Option is Selected]] | * [[:en:create:dynamic|Immediately Show Questions when a Certain Option is Selected]] |
| * [[:en:create:break-counter|Record Changes of Browser Window]] | * [[:en:create:break-counter|Record Changes of Browser Window]] |
| | * [[:en:create:script-keys]] |
| |
| |
| ===== Troubleshooting ===== | ===== Troubleshooting ===== |
| |
| If the JavaScript code does not work as it should, there is the browser's error console for debugging. These can be found -- depending on the browser -- in different places ([[http://webmasters.stackexchange.com/a/77337/30902|Stackexchange: Shortcuts]], [[https://codex.wordpress.org/Using_Your_Browser_to_Diagnose_JavaScript_Errors#Step_3:_Diagnosis|Wordpress: Diagnosis]], [[http://www.stramaxon.com/2013/10/developer-tools-in-browser.html|How to Open Developer Tools]]). | If the JavaScript code does not work as it should, there is the [[:en:general:browser-tools|browser's error console]] for debugging. |
| |
| In the error console you can see errors that occured while running the code. You can also display what is stored in a JavaScript variable by using `console. log ()`. The following lines identify an input field with the HTML ID "AB01_01" and then use the console to display whether it was found. | In the error console of the browser developer tools you can see errors that occured while running the code. You can also display what is stored in a JavaScript variable by using `console.log()`. The following lines identify an input field with the HTML ID "AB01_01" and then use the console to display whether it was found. |
| |
| <code javascript> | <code javascript> |
| ===== Learn JavaScript ===== | ===== Learn JavaScript ===== |
| |
| There are a number of good introductions to JavaScript on the internet. However, basic programming knowledge is needed beforehand. | Didactically prepared is the following 30-hour free course from Duke University: [[https://www.coursera.org/learn/duke-programming-web?#syllabus|Programming Foundations with JavaScript, HTML and CSS]] |
| | |
| | In addition, you can find several JavaScript beginner courses on the Internet, e.g. [[https://www.javascript-kurs.de/|JavaScript lernen für Anfänger]] or [[https://lerneprogrammieren.de/javascript/|JavaScript Lernen: So lernst du es schnell & einfach]] |
| |
| [[http://de.selfhtml.org/javascript/index.htm|SelfHTML]] is a website that combines a solid introduction with an extensive compendium [[http://de.selfhtml.org/javascript/index.htm|SelfHTML]] (German only). | [[http://de.selfhtml.org/javascript/index.htm|SelfHTML]] (German only) combines a compact introduction with an extensive compendium. However, you should already have some basic idea of programming. |
| |