App

Using onAssessment Callback

The onAssessment callback allows you to run custom code as soon as Monocle completes its assessment.

Option 1: Configuration Property

Pass an onAssessment function when initializing Monocle:

plaintext
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:

plaintext
<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:

  1. The configuration property callback runs first
  2. The script tag attribute callback runs second

Blocking Form Submission Until Ready

A common pattern is to disable form submission until the assessment has completed. This ensures the assessment data is included with the submission.

plaintext
<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():

plaintext
var assessment = monocle.getAssessment();

This returns the current assessment string, or undefined if one has not yet been received.