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
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:
- How do I cancel my account so I can get a new one?Best solution by Yahoo! Answers
- How can I hook up a Dual Tuner dvr to a second tv?Best solution by Yahoo! Answers
- How do I reply to a welcome e-mail for my new job?Best solution by Yahoo! Answers
- How can I get an audio alert when I get a new e-mail?Best solution by Yahoo! Answers
- How do I hook up a VCR/DVD combo to a DVD recorder and TV?Best solution by techwalla.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.