Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Passing a variable from study to study in JATOS via URL

Dear All,

Where can I find the value of a variable in JATOS if I transfer it from the first study to the second study via URL (for example, with the command window.location.href = "https://jatos.mindprobe.eu/publix/tT100ELC7xs?id= " + 77777;) and how can I write it in the results in another study?

Thanks for reply,

Marko

Comments

  • edited May 2024

    Hi @marko ,

    If the experiment is hosted on a JATOS server, URL parameters are available in jaros.urlQueryParameters . If the experiment is running as a standalone HTML file (e.g. during testing), the parameter is available through the URLSearchParams class.

    The example below shows how you can get the task URL parameter in a way that works both when running the experiment through JATOS and when running it standalone. (The script assumes that task is a number.)

    // Check if the task was passed a url parameter through JATOS
    if ((typeof jatos !== 'undefined') && ('task' in jatos.urlQueryParameters)) {
        task = Number(jatos.urlQueryParameters['task'])
        console.log(`task ${task} was specified as a JATOS URL query`)
    // Else check if it was passed as a regular parameter (for testing)
    // and fall back to 0 if no parameter was specified.
    } else {
        const urlParams = new URLSearchParams(window.location.search)
        task = Number(urlParams.get('task') || 0)
        if (task === 0) {
            console.log('no task was specified')
        } else {
            console.log(`task ${task} was specified as a regular URL query`)
        }
    }
    

    Hope this helps!

    — Sebastiaan

  • I agree with Sebastian and just want to answer your second question: how to write it in the results of the second study: Use on of the functions jatos.submitResultData or jatos.appendResultData.

    Best,

    Kristian

  • Dear Sebastian and Kristian,

    Thank you for the answer. Now I see that I have another related question: How can I insert the value of some variable from the first experiment into the URL. So, if in the first experiment I generate the value of the variable "idisp", how can I transfer the value of that variable through the URL to the next experiment. Experiments are linked in JATOS via the "End Redirect URL" option.

    Thank you again,

    Marko

  • Hi Marko,

    Somehow your second question got lost. It's better to start a new question in the forum so this doesn't happen and also so other people can find it easier.

    You would use jatos.endStudyAndRedirect to end your study, redirect it to a different page and pass on information via the URL query parameters. The "End Redirect URL" option in the study properties does the same but I guess you want to pass a parameter that only exists in your code.

    Here is an example

    1. You'd get a study link to your second study, e.g. https://www.example.com/base/publix/Hbr4mxMnyjt. You probably have to use a type 'general' study link, because it's difficult to use individual links in this scenario.
    2. Add the URL query parameter to it, e.g. if your parameter is myParameter=1234ABCD you'd have the URL https://www.example.com/base/publix/Hbr4mxMnyjt?myParameter=1234ABCD .
    3. Then use jatos.endStudyAndRedirect in your first study to redirect to your second study:
    jatos.endStudyAndRedirect("https://www.example.com/publix/Hbr4mxMnyjt?myParameter=1234ABCD");
    

    You can pass on as many parameters as you want, just separate them by '&'.

    Best,

    Kristian

Sign In or Register to comment.