Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Getting the token from javascript

More
6 years 2 weeks ago #187475 by LarryMartell
Is there a way to access the survey token from my javascript code?
The topic has been locked.
More
6 years 2 weeks ago #187476 by jelo
Expression script (aka ExpressionManager) variables are available as strings inside javascript. No objects.

Where do you run your Javascript? Try to assign {TOKEN} as a string to a javascript variable inside your JS code.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The following user(s) said Thank You: DenisChenu
The topic has been locked.
More
6 years 2 weeks ago #187495 by LarryMartell
Not sure what you mean by your question. In the browser source tab the JS is running from tmp/assets. Accessing TOKEN gives Uncaught ReferenceError: TOKEN is not defined
The topic has been locked.
More
6 years 2 weeks ago #187496 by jelo
What version of LimeSurvey is used? Might be a bug or something was changed in a newer version. Is the Survey activated when you test TOKEN?

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
More
6 years 2 weeks ago #187498 by LarryMartell
Version 2.73.1+171220. Yes I am taking the survey and I have a break point set in the JS code $(document).ready function and I am checking it there in the debugger.
The topic has been locked.
More
6 years 2 weeks ago - 6 years 1 week ago #187499 by tpartner
{TOKEN} like all placeholders is rendered at the server level so is only available in JS placed in survey text elements, questions group descriptions or theme TWIG files.

Placeholders are not available in the custom.js file and, unless previously defined, the console.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 6 years 1 week ago by tpartner.
The topic has been locked.
More
6 years 2 weeks ago #187503 by jelo

LarryMartell wrote: Not sure what you mean by your question.

Tpartner answered that nicely. Sorry, that I didn't realized you're using an external script.

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
More
6 years 2 weeks ago #187505 by LarryMartell
Ah, the token is in the URL and I can get that in the JS.
The topic has been locked.
More
6 years 2 weeks ago #187506 by tpartner
That will probably only work on the first screen or if using AJAX mode (which I advise against).

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
6 years 2 weeks ago #187510 by LarryMartell
What about the attributes? Can I access those in my JS?
The topic has been locked.
More
6 years 2 weeks ago #187513 by tpartner
Token attributes are rendered exactly the same as the token.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
More
6 years 1 week ago #187752 by DenisChenu
Easy way : add a javascript function in github.com/LimeSurvey/LimeSurvey/blob/ma...r/custom_header.twig

Something like
Code:
<script>
var token = "{TOKEN}";
var surveyId = "{SID}";
var yourVarComeFromSurvey = "{QuestionCode}";
</script>

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. - Professional support - Plugins, theme and development .
I don't answer to private message.
The following user(s) said Thank You: tpartner
The topic has been locked.
Moderators: holchtpartner

Lime-years ahead

Online-surveys for every purse and purpose