Ways to Open a New Page in JavaScript

This post summarizes ways to open a new page in JavaScript, with notes on behavior and practical considerations.

Dec 20, 2023 · 1 min read · 129 Words · -Views -Comments · Programming
JavaScript 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

Frontend code often needs to open new pages. Here is a summary of methods and issues encountered.

Methods

  1. Anchor tag
  2. window.open

window gives finer control, such as width and height. For anchor tags, you need to create a DOM element and decide whether it is visible. Anchor tags are generally preferred because they represent user-driven interactions.

Behavior in PWA

A PWA is just a presentation mode for web pages. If you open a new page in a PWA, the behavior differs:

  1. Anchor tags open in the traditional browser UI.
  2. window.open follows the PWA display mode. For example, if the PWA uses standalone, the new window also uses standalone.

In my opinion, in PWAs you should use location.href rather than opening a new tab. Try to keep an app-like experience.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover