Waiting for the Assessment
Any action that depends on the Monocle assessment should wait for it to complete. For most users, the assessment finishes almost instantly. When Monocle detects signals that a user may be anonymizing themselves, it takes longer to gather the additional evidence needed for a confident call. If the action fires before the assessment is ready, it goes through with an empty assessment, which defeats the point of running Monocle in the first place.
The onAssessment callback runs as soon as the assessment is ready, giving you a reliable point to unblock the action, attach the assessment to an outgoing request, or trigger downstream validation. A form submission is the most common case, but the same pattern applies to logins, checkout flows, API calls, or anything else you want to condition on the assessment.
Option 1: Configuration Property
Pass an onAssessment function when initializing Monocle:
monocle.init({onAssessment: function (assessment) {console.log("Assessment received:", assessment);}});
Option 2: Script Tag Attribute
Set the onassessment attribute on the Monocle script tag, pointing to a globally available function:
<script src="https://..." onassessment="handleAssessment"></script><script>function handleAssessment(assessment) {console.log("Assessment received:", assessment);}</script>
The function must be globally accessible on window.
Using Both Together
Both methods can be used at the same time.
When an assessment is returned:
- The configuration property callback runs first
- The script tag attribute callback runs second
Example: Gating a Form Submission
A common application of this pattern is to disable a form's submit button until the assessment has completed, ensuring the assessment data is available when the form is submitted.
<form id="myForm"><button id="submitBtn" type="submit" disabled>Submit</button></form><script>monocle.init({onAssessment: function (assessment) {document.getElementById("submitBtn").disabled = false;}});</script>
Start with the submit button disabled, then enable it inside the onAssessment callback once the assessment is ready.
Note: This is a UX convenience, not a security measure. Always validate the assessment on your backend using the Monocle API before trusting the result.
Retrieving the Assessment Later
To access the assessment outside of the callback, use getAssessment():
var assessment = monocle.getAssessment();
This returns the current assessment string, or undefined if one has not yet been received.