Adding Javascript? | XM Community
Solved

Adding Javascript?

  • 14 October 2019
  • 2 replies
  • 200 views

Hello! I am new to computer programming and hoping to figure out how to add some Javascript to the survey I'm running for my lab.

I got the code from here: https://developers.google.com/recaptcha/docs/v3


<script src="https://www.google.com/recaptcha/api.js?render=_reCAPTCHA_site_key"></script>
<script>
grecaptcha.ready(function() {
grecaptcha.execute('_reCAPTCHA_site_key_', {action: 'homepage'}).then(function(token) {
...
});
});
</script>

When I try to save the pasted code into the "Add Javascript" box, I get an error message ("Unexpected token <"). Clearly I'm out of my depth, but my hope is that getting this code to work with our survey might be a fixable problem. I think this bot-detection functionality may be available on Qualtrics, but our school account doesn't seem to have access to this user-friendly version of the feature.

I would so appreciate any help or advice you can spare. Thank you for your time!
icon

Best answer by KoalaTricks 15 October 2019, 17:22

View original

2 replies

Userlevel 4
Badge +3
It sounds like you pasted all of the code from the first snippet at the URL, which is HTML (it has the <>) and not pure JavaScript.

You would want to put the first line in the header of your survey (the source editor, not the WYSIWIG editor):
```
<script src="https://www.google.com/recaptcha/api.js?render=_reCAPTCHA_site_key"></script>
```

And then only this part on the question-level "Add Javascript" where you want it to appear. I am assuming you already have the requisite site key and token -- it won't work otherwise.

```
grecaptcha.ready(function() {
grecaptcha.execute('_reCAPTCHA_site_key_', {action: 'homepage'}).then(function(token) {
...
});
});
```
Thank you so much!

Leave a Reply