How open a new tab in browser without blocking?

2.1k Views Asked by At

Haxe/OpenFL code:

import openfl.net.URLRequest;
import openfl.Lib;

Lib.getURL (new URLRequest (url), "_self");  
// Opens the linked document in the same window or tab as it was clicked

Lib.getURL (new URLRequest (url), "_blank"); 
// Opens the linked document in a new window or tab. (this is default)

However, the second option generate popup that is blocked by Chrome.

How to open a link in another tab without being blocked?

With Javascript this work:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<title>OpenNewTab</title>

<meta id="viewport" name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes">

</head>
<body>  

<center>
    <canvas id="myCanvas" width="200" height="200" style="border-style: solid; border-width: 1px"> </canvas>
</center>

<script>   

    var canvas;
    var linkURL = "http://www.google.com";        

    createLink();

    function createLink() {

        canvas = document.getElementById("myCanvas");           
        canvas.addEventListener("click", Link_click, false);

    }

    function Link_click(e) {

         window.open(linkURL,'_blank'); 

    }     
</script>

</body>
</html>

P.s: I use Stencyl and HTML/JavaScript.

2

There are 2 best solutions below

2
On

I believe if the popup is opened from a user triggered event (like pointer down, click) no popupblocker should prevent opening it.

note: Personally I find it annoying that a developer decides how a window should be opened, why not let the user decide that theirselves?

1
On

While I do not find better solution I will use this:

import openfl.net.URLRequest;
import openfl.Lib;

class Web
{

public static function open(s:String, code:Int)
{   
    var type:String = "_self";
    var s:String = s;
    var code:Int = code;

    if(code==1){
        type = "_self";
    }else if(code==2){
        type = "_blank";
    }  

#if js      
         untyped __js__('

                var canvas;
                var linkURL = s;
                var lock = 0;

                if(lock==0){
                   lock =1;
                   createLink();
                }

                function createLink() {

                    canvas = document.getElementById("openfl-content");           
                    canvas.addEventListener("click", Link_click, false);

                }

                function Link_click(e) {

                     window.open(linkURL,type); 

                }       

        ');             

#else
     Lib.getURL (new URLRequest (s), type);   
#end
}

}