Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subtitlesMenu redeclaration in "Adding captions and subtitles to HTML video" tutorial #37128

Merged
merged 2 commits into from
Dec 9, 2024

Conversation

jacob-willden
Copy link
Contributor

Description

In the "Building a caption menu" section of the "Adding captions and subtitles to HTML video" tutorial, the subtitlesMenu variable is accidentally declared twice:

let subtitlesMenu;
if (video.textTracks) {
	const df = document.createDocumentFragment();
	const subtitlesMenu = df.appendChild(document.createElement("ul"));
	/* ... */
}

Using this verbatim leads to the following error: Uncaught ReferenceError: can't access lexical declaration 'subtitlesMenu' before initialization. Removing the const for subtitlesMenu turns the statement from an unintended 2nd declaration to the intended initialization of the 1st declaration.

Motivation

Readers who are going through this tutorial would be confused that using the tutorial code is leading to browser errors. This pull request will prevent confusion for future readers.

Additional details

The MDN tutorial is based on code from a tutorial by iandevlin, which uses the ECMAScript 5 syntax of var instead of let and const:

var subtitlesMenu;
if (video.textTracks) {
	var df = document.createDocumentFragment();
	var subtitlesMenu = df.appendChild(document.createElement('ul'));
	/* ... */
}

While using let and const as above means 2 different subtitlesMenu variables are stored, using var means only 1 subtitlesMenu variable is stored and it is assigned df.appendChild(document.createElement('ul')).

Related issues and pull requests

Having done a quick search, it appears there are no related issues or pull requests.

@jacob-willden jacob-willden requested a review from a team as a code owner December 6, 2024 22:33
@jacob-willden jacob-willden requested review from chrisdavidmills and removed request for a team December 6, 2024 22:33
@github-actions github-actions bot added Content:Media Media docs size/xs [PR only] 0-5 LoC changed labels Dec 6, 2024
@jacob-willden jacob-willden changed the title Fix subtitlesMenu reassignment in "Adding captions and subtitles to HTML video" tutorial Fix subtitlesMenu redeclaration in "Adding captions and subtitles to HTML video" tutorial Dec 6, 2024
Copy link
Contributor

github-actions bot commented Dec 9, 2024

Preview URLs

(comment last updated: 2024-12-09 10:36:41)

Copy link
Contributor

@chrisdavidmills chrisdavidmills left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks @jacob-willden!

@chrisdavidmills chrisdavidmills merged commit 1e25b7f into mdn:main Dec 9, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:Media Media docs size/xs [PR only] 0-5 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants