How open a new tab in browser without blocking?

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 open a new window in browser without blocking?

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.

Not sure the answer is the code aboveā€¦ Typically Chrome and other browsers will block any new Window that is not invoked by a user action, such as clicking a button or link.

Thanks for reply. Does that exist any way to inject a link or div on the page as in Angular 2?

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

}

Does it work if you call it in direct response to a mouse event?

This need a addEventListener to not be considered a popup.
I do not know how make with haxe. But this code works fine to html5.

I think this would do the same:

addEventListener (MouseEvent.CLICK, function (_) {
   Lib.getURL (new URLRequest (url));
});

If not, would be happy to look into why not :slight_smile:

Anyway! Thanks for sharing your solution, either way

1 Like

This should work.

But I not know how to select the ā€œcanvasā€ nor what library need import.
What I did not work and I am tired of it. :blush:

Thank you for reply