Saturday, April 18, 2020

Open Link in New Tab

All major browsers use popup blockers to prevent sites from opening new tabs. Popup blockers works in slightly different ways and block popups in different contexts.

First chapter gives short overview of popup blockers. The rest of article shows multiple ways how to open new tab and how to work around browsers limitations.

Table Of Contents

Popup Blockers

A browser does not distinguish between a new tab and a popup. Therefore popup blockers treat them the same way.
and in this article I use the terms "new tab" and "new window" interchangeably.

Popup blocker built inside the Firefox is willing to open new tab only from onclick hander. It prevents tab opening from mouse over event, network request response, and from inside script in the body. Instead of opening new tab, firefox will show yellow warning banner which says Firefox prevented this site from opening popup. It is possible to turn off the warning, but that requires user action.

Edge works similarly to Firefox, except that banner is white and show on bottom of the window.

Chrome always prevents new tabs from network request response. It mostly prevents also new tabs from on mouse over event and script inside the body. The last two are sometimes created, but I did not found when exactly. Chrome shows small icon in url bar when it refuses to open the new tab. User can click on it and allow popups manually.

Html Link

The easiest way to create a link that opens in a new tab is to add target="_blank" to it. It works in Chrome, Firefox and Edge.
<a href="" target="_blank">Normal Link</a>
This is how it renders: Normal Link


JavaScript can open new tab using the, name, specs, replace) function. The trick is to put '_blank' into name argument.'', '_blank');
This works in most, but not all, situations. Most importantly, Edge and Firefox block new tab from network request response handler. When the url have to be loaded from backend, Firefox requires tricks to work.

This chapter has two sections. First section deals with situation when the frontend knows the url. Second section shows how to open link in new tab when the fronted needs to send network request.

When the Url is Known
In this chapter, we will try to open a new tab from mouse click, from timer, from mouse over and from a script inside the body.

From Click
Open new tab from onclick attribute of html tag works from Chrome, Firefox and Edge:
<a href="#" onclick="'', '_blank');
    return false;
">on click</a>
This is how it renders: on click

From Timer Inside Click
You can also start a timer while handling users onclick method and open a new tab later from the timer. It works in Chrome, Firefox and Edge:
<a href="#" onclick="
    setTimeout(() =>'', '_blank'), 500);
    return false;
">click starts timer</a>
This is how it renders: click starts timer

From Mouse Over
Open new tab from onmouseover attribute of an html tag does not work. Firefox and Edge block it all the time. Chrome blocks it most of the time, although blocking is not perfect and the tab occasionally shows up:
<a href="#" onmouseover="'', '_blank');
    return false;
">on mouse over</a>
This is how it renders: on mouse over

From Timer Inside Mouse Over
Open new tab from timer started inside onmouseover does not work. Firefox and Edge block it all the time. Chrome blocks it most of the time, although blocking is not perfect and the tab shows up once in a while:
<a href="#" onmouseover="
setTimeout(function() {'', '_blank');
} ,500);
return false;
">timer on mouseover</a></br>
This is how it renders: timer on mouseover

From Body Onload
Opening new tab from script in body is blocked by all three browser. Add this inside the body tag to see it:
 setTimeout(function() {
  console.log("running");'', '_blank');
 }, 500);

Fetching Url From Backend
More interesting and useful case is when the web application needs to fetch the url from backend. The straightforward solution does not work, Edge and Firefox block the new tab. However, this use case is too useful to be ignored, so we will build workaround.

From Request Response
Opening new tab from request response works only in Chrome. Edge and Firefox shows yellow warning and prevents new tab to open:
function fromHttpRequest() {
 const Http = new XMLHttpRequest();
 const url='';"GET", url);

 Http.onreadystatechange = (e) => {
   console.log(Http.responseText)'', '_blank');
<a href="#" onclick="
    return false;
">from http request</a></br>
This is how it renders: from http request

Starting timer from request response does not help. Chrome and Edge will open the tab, but Firefox will show warning instead. I have omitted the example, because it is long and almost the same as timers above.

Workaround 1
The workaround is to combine javascript interval started from user click, network request and let them communicate through variable.
  • Network request waits for response. Then it sets the common variable.
  • The interval periodically checks variable value. It opens new tab once the variable has the response. Then the interval removes itself.
function intervalListensForRequestResponse() {
 var iHaveTheResponse; // shared variable

 // send request
 const Http = new XMLHttpRequest();
 const url='';"GET", url);

 Http.onreadystatechange = (e) => {
   iHaveTheResponse = '';

 // periodically check the variable
 var refreshIntervalId = setInterval(function(){ 
    console.log('tick ' + iHaveTheResponse);
    if (iHaveTheResponse) {, '_blank')
  }, 500);
 return false;
<a  href="#" onclick="
    return intervalListensForRequestResponse();
">interval listens for request response</a>
This is how it renders: interval listens for request response

Workaround 2
If the frontend knows for sure it will have to open new tab and just needs to acquire correct url from backend, you can:
  1. Open new window and keep reference to it.
  2. Send the request.
  3. Set url to newly opened window.

The disadvantage of this solution is that new window is opened before the response from backend came back. There is a blank window for few miliseconds. In addition, it is impossible to make backend decide whether the window will be needed or not.
function startByOpeningTheWindow() {
 var newTab ="", '_blank');
 if (newTab==null) {
  return ;

 // send request
 const Http = new XMLHttpRequest();
 const url='';"GET", url);

 Http.onreadystatechange = (e) => {
   newTab.location.href = '';
 return false;
<a href="#" 
>first open the window, change url later</a></br>
This is how it renders: first open the window, change url later

Few Notes on Open Function
Communication between original window and new tab is possible if they have the same origin. If they show pages from different domains, the communication is blocked by the browser due to Same Origin Policy rules. It ends with Error: uncaught exception: Permission denied to get property error.

The function returns reference to the tab that was just opened. The original page can use it to call functions inside newly opened tab. If the Firefox popup blocker blocked the popup, the function returns null. You can use this to check whether the new tab was blocked. This method works only for built-in popup blocker, it does not work for ad-blocking addons.

New tab has the opener property which can be used to call functions inside the main window. It also has the closed property. Main window can use it to check whether the child tab was open or not.

All Examples

All tested cases are on this page.


Siddharth said...

This is very good information I like this blog keep it up

Telegram Porn Channel || uk whatsapp group link 2020 || indian whatsapp group link 2020


This is very good information I like this blog keep it up

delhi escorts said...

This is a good article, according to me, everyone should read this article and comment on it. I come to all the articles and try to learn something new.
delhi escorts
indian escorts
call girl

Unknown said...

nice article
IB IN INDIA said...

Bài viết rất hay: Chúng tôi chuyên cung cấp các sản phẩm chất lượng

Lều xông hơi tại nhà giá rẻ

Lều xông hơi mua ở đâu

Lều xông hơi giá bao nhiêu said...

Bài viết rất hay: Chúng tôi chuyên cung cấp các sản phẩm chất lượng

Giảo cổ lam giảm mỡ hiệu quả

Tìm hiểu giảo cổ lam khô hiện nay

Tác dụng thần kỳ của giảo cổ lam khô

Unknown said...

After reading your article I was amazed. I know that you explain it very well
Android Training Institute in Coimbatore Best Android Training Institutes in Coimbatore | Android Training Course in Coimbatore | Mobile App Training Institute in Coimbatore | Android Training Institutes in Saravanampatti | Online Android Training Institutes in Coimbatore | Mobile Development Training Institute in Coimbatore

Unknown said...

Really useful information. Thank you so much for sharing

IOT Training in Coimbatore | Best IOT Training institute in Coimbatore | Internet of Things Course Training in Coimbatore | Best Institutes for IoT Internet of Things Training in Coimbatore | IOT Training Course in Coimbatore | IOT Training in saravanampatti

Unknown said...

Thank you so much for shearing this type of post.
This is very much helpful for me. Keep up for this type of good post.

thefranchisegroup said...

It's really nice and meaningful. it's really cool blog, Thank you. Franchise Opportunities .
Business Franchise Opportunities in India
Food Franchise Opportunity

Kailey Robinson said...

Download Video Player | Windows Media Player | Best Media Player

William said...

All the time I found something new information from your blogs. Thanks for sharing .hey... Great work . I feel nice while i reading blog .You are doing well. Keep it up. We will also provide QuickBooks Customer Service Contact us +1-855-756-1077 for instant help.

RGB Webtech said...

Nice Blog ,your blog is sharing unique information....

We provide Web Design, Web Development, Digital Marketing (SEO, SEM, SMO, SMM), and Mobile App Development services.

* Web Design Services | Web Design Solutions
* Web Design Packages | Web Development Packages
* Technical SEO Checklist | Website Audit Checklist
* Online HTML Editor | Wysiwyg HTML Editor
* Career opportunities | Online Jobs From Home
* Web Designing Course | Free Online Courses with Certificates

For more information visit our website at Digital Marketing Services | Online Marketing Services

Reyansh said...

Useful for me.

up police bharti
cbse 10 maths sample paper

Rakesh said...
This comment has been removed by the author.
Rakesh said...

Jack Daniel said...

Your blog posts are more interesting and impressive. I think there are many people like and visit it regularly, including me. I appreciate your own position and I will be sure to come back here. embroidery digitizing

Phoebe Geller said...

Hey! Mind-blowing blog. Keep writing such beautiful blogs. In case you are struggling with issues on QuickBooks software, dial QuickBooks Customer Service Number The team, on the other end, will assist you with the best technical services.

MilesWeb said...

Are you looking for good hosting? So read Full Comparison of MilesWeb vs GoDaddy.

aditya said...

I read that Post and got it fine and informative.

garageband for pc
charles melton ethnicity
brooke simpson husband
Spy Dialer
free with edu email
aufbau principle

webseo said...

Immediately maintain a distance of 3 or 4 feet so that the electronics should not be a reason causing this error to you.

webseo said...

Is there an appropriate distance between your device and electronic

webseo said...

Is it showing connect Epson printer to WiFi? Just relax there is nothing to be tensed for, it is just a very common issue and can be fixed easily.

Zero Stock Brokerage said...

Thanks for sharing the important and awesome information, Thank you. How To Open Zerodha Account Opening Online , Zerodha Account Opening

thefranchisegroup said...

Your post was very nicely written, Thank you. Franchise Opportunities

Kailey Robinson said...

Thanks for the always useful information. This is great information to help peoples and nice article written by writer. CnX Player is a powerful & efficient 4K ultra HD enabled video player for Windows 10 PC & Tablet, Android and iOS – iPhone & iPad.

Download Media Player for Windows 10 - Microsoft Store
Download Video Player for Android from Google Play
Download Video Player for iPhone/iPad from Apple App Store

Post a Comment