# scrollTo and scrollBy: Two JavaScript APIs That Are Actually Somewhat Useful

# scrollTo and scrollBy: Two JavaScript APIs That Are Actually Somewhat Useful Author: zhangxinxu Published: July 27, 2019 ## 1. Is Scrolling Han Xin? The JavaScript APIs `scrollTo` and `scrollBy` are also used to control how far an element or window scrolls. After a quick searc...

Code: Dispatching a CustomEvent

const event = new CustomEvent('show', {
  detail: { message: payload, sentAt: new Date().toLocaleTimeString() }
});

window.dispatchEvent(event);
Result
Waiting for an event...

Code: Listening for the Event Payload

window.addEventListener('show', (event) => {
  render(event.detail.message, event.detail.sentAt);
});
Event log
No events yet.