Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex SmartCaptcha
  • Getting started
    • All guides
    • Creating a CAPTCHA
    • Deleting a CAPTCHA
    • Getting keys
    • Adding a widget using the advanced method
    • Verifying a CAPTCHA response
    • Viewing operations with resources
  • Access management
  • Pricing policy
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Release notes

In this article:

  • Add a SmartCaptcha widget using the advanced method
  • Add a SmartCaptcha widget as required
  • What's next
  1. Step-by-step guides
  2. Adding a widget using the advanced method

Add a SmartCaptcha widget

Written by
Yandex Cloud
Updated at July 12, 2024
  • Add a SmartCaptcha widget using the advanced method
  • Add a SmartCaptcha widget as required
  • What's next

Add a SmartCaptcha widget using the advanced methodAdd a SmartCaptcha widget using the advanced method

You control how the widget loads using the window.smartCaptcha object. The onloadFunction callback function is used for this in the guide:

  1. Add the JS script to the user page. To do this, place the following code anywhere on the page, for example, inside the <head> tag:

    <script
        src="https://smartcaptcha.yandexcloud.net/captcha.js?render=onload&onload=onloadFunction"
        defer
    ></script>
    
  2. Add an empty container where you want to install your widget, to the page:

    <div id="<container_ID>"></div>
    

    Where --id is an ad-hoc ID.

  3. Add the code of the callback function to the page:

    <script>
        function onloadFunction() {
        if (window.smartCaptcha) {
            const container = document.getElementById('<container_ID>');
    
            const widgetId = window.smartCaptcha.render(container, {
                 sitekey: '<client_key>',
                 hl: '<language>',
            });
        }
        }
    </script>
    

    Where:

    • <container_ID>: ID generated at the previous step.
    • sitekey: Client-side key.
    • hl: Widget and challenge language.

    Add a check for existence of the window.smartCaptcha object to avoid an error when the function is called before the JS script loading is complete.

    Note

    During the upload, the widget changes the height of its host container to 100px. This might result in an undesirable layout shift on the page due to the height change. To avoid this shift, set the 100px container height before the widget is loaded.

    <div ... style="height: 100px"></div>
    

Add a SmartCaptcha widget as requiredAdd a SmartCaptcha widget as required

To add a widget and load CAPTCHA as required, use the following approach:

window.onloadFunction = () => {
  if (window.smartCaptcha) {
    // Creating a CAPTCHA
  }
}

function handleScriptLoadingError() {
  // Handling errors
}

const scriptElement = document.createElement('script');
scriptElement.src = 'https://smartcaptcha.yandexcloud.net/captcha.js?render=onload&onload=onloadFunction';
scriptElement.onerror = handleScriptLoadingError;
document.body.appendChild(scriptElement);

What's nextWhat's next

  • Invisible CAPTCHA.
  • Connecting a CAPTCHA in React.

Was the article helpful?

Previous
Getting keys
Next
Verifying a CAPTCHA response
© 2025 Direct Cursus Technology L.L.C.