Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onbeforematch — handle an event before hidden content is revealed

The global onbeforematch attribute contains JavaScript handling the beforematch event. It lets code react before the browser reveals an element with hidden="until-found" during find-in-page or fragment navigation.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

onbeforematch="JavaScript statements"

onbeforematch is a global event handler attribute for beforematch. Its value is JavaScript statements, not an event name or a Boolean value. Its DOM counterpart is element.onbeforematch; an alternative is element.addEventListener('beforematch', handler).

When find-in-page or fragment navigation needs to reveal an element with hidden="until-found", the browser first fires beforematch, then removes hidden. The handler can therefore read the attribute while it still exists. The event bubbles and is not cancelable: preventDefault() does not prevent revealing.

onbeforematch does not itself hide content. Ordinary hidden does not provide the revealing mechanism available with hidden="until-found". Manually removing hidden is not that mechanism either and does not itself fire beforematch. Content Security Policy can block code in event handler attributes.

In the example, click Reveal target in a browser supporting this mechanism. The expected status text is beforematch: hidden=until-found; afterward, the content becomes visible and the hidden attribute is gone. Reload before repeating because the element has already been revealed.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>beforematch example</title>
<a href="#target">Reveal target</a>
<p id="status">Waiting</p>
<section hidden="until-found"
  onbeforematch="document.getElementById('status').textContent = 'beforematch: hidden=' + this.getAttribute('hidden')">
  <p id="target">The hidden target.</p>
</section>
</html>

Result

beforematch: hidden=until-found

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources