How do I open Javascript link in new tab?

How do I simulate clicking a link that opens in a new tab/window using jQuery/Javascript?

  • I have a link that opens in a new tab with _blank: <a href="new_page.html" target="_blank">link</a> I'd like to be able to 'click' on this from Javascript. I know I could do document.location=... but the problem here is the new tab part. Is this possible?

  • Answer:

    You can usually open a new window or tab using window.open. So instead of setting location, just call window.open and pass only the URL, nothing else.

izb at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

$("#your_a").click(function(e) { e.preventDefault(); window.open($(this).attr("href"), this.target); } You could use any name for target, preventDefault() if you want to override the link actions completely

Willy Carman

If you really want to simulate click, you can give a id to the anchor tag and call jQuery click() on that element. <a id="mylink" href="new_page.html" target="_blank">link</a> and in script $("#mylink").click()

R G

to "simulate" the click you trigger the click event: HTML: <a href="new_page.html" target="_blank" id="mylink">click me</a> jQuery: $("#mylink").trigger("click"); -or- $("#mylink").click(); Using 'trigger' allows you to pass in extra parameters if you bind a function to the click event. See the jQuery 'trigger' documentation for more info: http://api.jquery.com/trigger/

Mike Taylor

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.