Skip to content

Replace window.onLoad event listeners

Sarah German requested to merge 1718-clippy-awol into main

What does this MR do and why?

Fixes a regression I introduced in this MR: !4224 (merged)

This regression caused the "copy" buttons to not appear alongside code snippets.

How this broke:

The window.onLoad event listener can only be used once. If you have multiple functions listening on this, your first instance will be overridden by subsequent calls to it. In this case, when we added it for loading badges in !4224 (merged), it no longer fired for copy-to-clipboard.

To avoid this, we can use addEventListener on the same load event. This allows us to fire both functions (badges and clipboard copy) on that event, without overriding one another, and still running after tabs and the table of contents load.

Screenshots, screen recordings, or links to review app

Before After
image image

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

  1. Configure a local GitLab Docs environment: https://gitlab.com/gitlab-org/gitlab-docs/-/blob/main/doc/setup.md.
  2. Visit a page with a code sample (like this: http://localhost:3000/ee/administration/operations/rails_console.html)
  3. Verify the copy button shows up with code samples, and clicking it copies the contents of the code block.

Merge request acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Closes #1718 (closed)

Merge request reports