You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Learn how to add the Turnstile widget to your webpage using implicit or explicit rendering methods.
11
11
12
+
Choosing the appropriate rendering method for Turnstile is important and effectively helps you to add it to your website while ensuring optimal performance and user experience. Both implicit and explicit rendering have their own advantages and are suited to different types of web applications. Refer to the table below that highlights the ideal use case for each of these rendering methods:
|**Control over timing**| Renders automatically on page load | Full control over rendering timing |
18
+
|**Use cases**| Static content | Dynamic or interactive content |
19
+
|**Customization**| Limited to HTML attributes | Extensive via JavaScript API |
20
+
21
+
12
22
## Prerequisites
13
23
14
24
Before you begin, you must have:
@@ -28,7 +38,7 @@ Before you begin, you must have:
28
38
29
39
## Implicit rendering
30
40
31
-
Implicit rendering automatically scans your HTML for elements with the `cf-turnstile` class and renders widgets when the page loads.
41
+
Implicit rendering automatically scans your HTML for elements with the `cf-turnstile` class and renders widgets without additional JavaScript code. This set up is ideal for static pages where you want the widget to load immediately when the page loads.
32
42
33
43
### Use cases
34
44
@@ -43,7 +53,7 @@ Cloudflare recommends using implicit rendering on the following scenarios:
43
53
44
54
#### 1. Add the Turnstile script
45
55
46
-
Add the Turnstile JavaScript API to your HTML.
56
+
**Include the Turnstile Script**: Add the Turnstile JavaScript API to your HTML file within the `<head>` section or just before the closing `</body>` tag.
47
57
48
58
```html wrap
49
59
<script
@@ -75,7 +85,7 @@ Add widget containers where you want the challenges to appear on your website.
75
85
76
86
#### 3. Configure with data attributes
77
87
78
-
[Customize your widgets](/turnstile/get-started/client-side-rendering/widget-configurations/) using data attributes.
88
+
[Customize your widgets](/turnstile/get-started/client-side-rendering/widget-configurations/) using data attributes. Insert a `div` element where you want the widget to appear.
79
89
80
90
```html
81
91
<div
@@ -87,9 +97,12 @@ Add widget containers where you want the challenges to appear on your website.
87
97
></div>
88
98
```
89
99
100
+
Once a challenge has been solved, a token is passed to the success callback. This token must be validated against our [Siteverify endpoint](/turnstile/get-started/server-side-validation/).
101
+
90
102
### Complete implicit rendering examples by use case
91
103
92
104
<Detailsheader="Basic login form">
105
+
Turnstile is often used to protect forms on websites such as login forms or contact forms. You can embed the widget within your `<form>` tag.
93
106
```html title="Example"
94
107
<!DOCTYPE html>
95
108
<html>
@@ -110,6 +123,32 @@ Add widget containers where you want the challenges to appear on your website.
110
123
</body>
111
124
</html>
112
125
```
126
+
127
+
An invisible input with the name `cf-turnstile-response` is added and will be sent to the server with the other fields.
128
+
129
+
```html title="Complete HTML example"
130
+
<!DOCTYPE html>
131
+
<htmllang="en">
132
+
<head>
133
+
<metacharset="UTF-8">
134
+
<title>Implicit Rendering with Cloudflare Turnstile</title>
@@ -201,7 +240,7 @@ When you embed a Turnstile widget inside a `<form>` element, an invisible input
201
240
202
241
## Explicit rendering
203
242
204
-
Explicit rendering gives you programmatic control over when and how widgets are created using JavaScript functions.
243
+
Explicit rendering gives you programmatic control over when and where the widget appears and how the widgets are created using JavaScript functions. This method is suitable for dynamic content, single-page applications(SPAs), or conditional rendering based on user interactions.
After rendering the Turnstile widget explicitly, you may need to interact with it based on your application's requirements. Refer to the sections below to manage the widget's state.
290
+
291
+
#### Reset a widget
292
+
293
+
To reset the widget if the given widget timed out or expired, you can use the function:
0 commit comments