Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 9
    Thank you. Your pure-JS has a missing piece -- as Luke Alderton wrote (see below), you need to attach created element to the document body, in your code it is hanging in the void an at least in Firefox 37 it does not do anything. Commented May 24, 2015 at 6:55
  • 7
    @mcginniwa Any action like this - if not triggered by user action like mouse click will turn popup-blocker. Imagine that you can just open any url with Javascript without user action - that would be quite dangerous. If you put this code inside event like click function - it will work fine - it's the same with all proposed solutions here. Commented Oct 8, 2015 at 12:11
  • 5
    You can simplify your code as follows: $('<a />',{'href': url, 'target', '_blank'}).get(0).click(); Commented Dec 22, 2017 at 17:20
  • 15
    @shaedrich inspired by your snipped I added non-jquery one-liner: Object.assign(document.createElement('a'), { target: '_blank', href: 'URL_HERE'}).click(); Commented Jan 5, 2018 at 12:07
  • 1
    Using target="_blank" without rel="noreferrer" and rel="noopener" makes the website vulnerable to window.opener API exploitation attacks (vulnerability description), although note that, in newer browser versions setting target="_blank" implicitly provides the same protection as setting rel="noopener". developer.mozilla.org/en-US/docs/Web/HTML/Element/A Commented Jun 10, 2022 at 19:54