HOWTO · JavaScript

How to Auto Click in JavaScript

Open a link in another browsing context with window.open(), or navigate the current page with window.location.

On this page

“Auto click” can mean opening a link when code runs or activating the link itself. Use window.open() to request the destination in another browsing context, or window.location to navigate the current one. Neither method simulates an anchor click or runs its click handlers; call the anchor element’s click() method only when you specifically need programmatic click activation.

Use window.open() to Auto Click in JavaScript

window.open() requests a URL in a new or named browsing context. The browser decides whether that context appears as a tab or a window, and it may block a script-initiated request unless it is allowed by browser policy, often because it follows a user action. It does not trigger the destination anchor’s click handlers.

For example, suppose the page has an anchor such as <a id="docs-link" href="https://example.com/">Documentation</a>. Read its resolved href and pass it to window.open():

if (typeof document !== "undefined") {
  const link = document.getElementById("docs-link");
  if (link) {
    window.open(link.href, "_blank", "noopener");
  }
}

The name _blank asks for a separate browsing context, but browser settings determine its exact presentation and may block the request. The noopener feature prevents the opened page from receiving a usable window.opener reference. This browser example is unexecuted here; it requires a page containing the anchor and a browser environment.

Use window.location to Auto Click in JavaScript

To navigate the current browsing context to the same link, assign its destination to window.location or window.location.href, or call window.location.assign():

if (typeof document !== "undefined") {
  const link = document.getElementById("docs-link");
  if (link) {
    window.location.assign(link.href);
  }
}

Each option performs navigation in the current context; it does not open another tab or activate the anchor’s click handlers. Window.location is a property that returns a Location object, which exposes the current URL and navigation methods. The global location name is also commonly available in browser scripts, but window.location makes the owner explicit.

For API details, see the MDN documentation for the Window.location property. Use an anchor’s click() method if the task is to activate that element and its click behavior; programmatic activation does not represent a physical user action and is subject to browser behavior.