Welcome to the LimeSurvey Community Forum

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

Getting the token from javascript

  • LarryMartell
  • LarryMartell's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 8 months ago #187475 by LarryMartell
Getting the token from javascript was created by LarryMartell
Is there a way to access the survey token from my javascript code?
The topic has been locked.
More
4 years 8 months ago #187476 by jelo
Replied by jelo on topic Getting the token from javascript
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.
  • LarryMartell
  • LarryMartell's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 8 months ago #187495 by LarryMartell
Replied by LarryMartell on topic Getting the token from javascript
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
4 years 8 months ago #187496 by jelo
Replied by jelo on topic Getting the token from javascript
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.
  • LarryMartell
  • LarryMartell's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 8 months ago #187498 by LarryMartell
Replied by LarryMartell on topic Getting the token from javascript
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.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 8 months ago - 4 years 8 months ago #187499 by tpartner
Replied by tpartner on topic Getting the token from javascript
{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: 4 years 8 months ago by tpartner.
The topic has been locked.
More
4 years 8 months ago #187503 by jelo
Replied by jelo on topic Getting the token from javascript

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.
  • LarryMartell
  • LarryMartell's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 8 months ago #187505 by LarryMartell
Replied by LarryMartell on topic Getting the token from javascript
Ah, the token is in the URL and I can get that in the JS.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 8 months ago #187506 by tpartner
Replied by tpartner on topic Getting the token from javascript
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.
  • LarryMartell
  • LarryMartell's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 8 months ago #187510 by LarryMartell
Replied by LarryMartell on topic Getting the token from javascript
What about the attributes? Can I access those in my JS?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 8 months ago #187513 by tpartner
Replied by tpartner on topic Getting the token from javascript
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.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 8 months ago #187752 by DenisChenu
Replied by DenisChenu on topic Getting the token from javascript
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 service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: tpartner
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose