How can you use JS to turn a relative URL into an absolute URL?

How can you use JS to turn a relative URL into an absolute URL? In real project development, you may run into a situation where you have a relative URL and want to convert it into an absolute URL. For example: “/wordpress/?p=9227” → “https://www.zhangxinxu.com/wordpress/?p=9227”...

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.