Lesson 4 of 4 · 15 min read
JavaScript: making pages respond
JavaScript listens for events (clicks, typing, scrolling) and changes the page in response:
const btn = document.querySelector('#menuBtn');
btn.addEventListener('click', () => {
document.body.classList.toggle('menu-open');
});
Golden rules for safe JavaScript:
- Insert user text with
textContent, neverinnerHTML— this blocks XSS attacks. - Never put API keys in JavaScript — anyone can read them. Keep secrets on the server (PHP).
Next step: take the PHP & MySQL course to build the server side.
⚡ Quick check
Safest way to show text a user typed?
textContent never interprets the text as HTML, so injected scripts can’t run.