- Home /
Unity web player opening link in new tab
Hi,
Am currently working on a client project in which I am required to open a link in new tab. I tried using
Application.OpenUrl("www.mylink.com");
where in the page opens up in the current tab. I also tried
Application.ExternalEval("window.open('www.mylink.com')'")
where the page opens in a new pop-up window and is caught by the browsers popup blocker.
Is there any other work around to open links via unity web player?
Thanks in advance.
To be short: are there any plans to add a built-in option in Unity API to open a link in a new browser tab?
Let me explain. I couldn't make JavaScript solution to work. Either I misunderstood it, or Unity Web player has been changed since the date of ytc's response, but JavaScript doesn't receive any mouse events for Unity player container in Firefox and Chrome for me.
I tried to listen the events in a transparent div element covering the whole player, but it doesn't receive any events either. I tried to produce a workaround placing my link in an opaque jQuery UI dialog, and it works perfectly in Firefox, but in Google Chrome it is completely overlapped by Unity player.
I understand that changing Unity API to support this feature is difficult to make because of security issues, but it would be great.
Answer by Graham-Dunnett · Jun 05, 2013 at 02:02 PM
Get the webplayer to talk to the hosting webpage, and then have Javascript in the html page do what you want.
See: http://docs.unity3d.com/Documentation/Manual/UnityWebPlayerandbrowsercommunication.html
that probably is the best way to go abt it. :) but I am not too familiar with javascript. When I deliver the build I just need to upload the .unity3d file and the client does the rest.
Any idea how to implement this?
Answer by Stormizin · Jun 05, 2013 at 02:25 PM
Application.OpenUrl("http://www.google.com");
This code in production mode(compiled for webplayer) already open that link ina a new tab.
yeah i tried that, but it opens the link in the current tab. So my current game is lost :(
Dude, there is no built-in support for this, so yes, you need to implement this using Application.ExternalCall
You can use something like:
Application.ExternalEval("window.open('http://your http://site.com','Window title')");
check my original post! I tried tat, but it opens up the page as a pop-up and most browsers have pop-up blockers enabled :(
Popup blockers will block any attempt to open a new tab/window through pure javascript.
The only way to get around it is by calling window.open() on a user event, such as the onclick event..
Application.ExternalEval("var link=document.getElementById('newtablink'); link.onclick=function() { window.open('http://site.com','_blank') }");
This means you would need the user to click a link on the page, to avoid the pop up blocker denying your request.
Answer by BenoitFreslon · Jun 25, 2014 at 10:41 AM
Still impossible to open a new tab in Unity 4.5 ?
Thanks.
Answer by ytc · Jul 10, 2014 at 12:11 AM
finally figure out a way to do it, not sure it will be working in all cases. have to use Application.ExternalCall in unity and javascript in the page hosting the unity webplayer.
in untiy on the object, add a script (in C#) to trigger on/off signal using a int:
void OnHover (bool isOver) {
//originally using onClick, but it didnt work well with mouseup with the webpage javascript, any one can try using onMouseUp etc other than Hover
if (isOver) {
Application.ExternalCall ("OpenInNewTab", 1);
}else {
Application.ExternalCall ("OpenInNewTab", 0);
}
}
the javascript as follow:
function OpenInNewTab(val) {
var link=document.getElementById('divID');//the div id enclosing the webplayer
link.onmouseup=function() { //originally trigger the signal by onClick in unity, but later i shift to trigger through hover of the div
if(val==1){ window.open('siteurl');
val=0;
}else{
// console.log("no");
}
}}
hope this helps.
I don't see how this differs to the previous suggestions seeing as you still need to click an item on the html page to prevent the new page being blocked by pop-up blockers..
Although you could try checking for the onmouseover event of the entire html element in combination with your "val" variable approach. So the onmouseover event would be called constantly but you would only call window.open() if val was 1.
Let me know if I misunderstood your solution :D
um...well, you can see it as an elaboration/ example from the above suggestions. Yes, still need the click event, trigger other than click, like mousemove etc. still causing a pop up window. I think this part is about the javascript behaviour, you can dig more or google for solution. http://stackoverflow.com/questions/15818892/chrome-javascript-window-open-in-new-tab
I have tried all the above suggestions, found even with a click event, any call of action on window opening from the unity webplayer will either create a pop-up window or open in the current, so I let the html handle the click event and the url opening. Since my case is to open a link through a button click in the unity, the val 1 / 0 triggering from the unity is aimed for the object specific when the click event happen.(@merry_christmas, you got it right)
Answer by teophilik · Sep 27, 2016 at 12:09 PM
Try this method and you wont have any popup block problems. It worked for me https://www.youtube.com/watch?v=_D5deiE_r9c
Your answer
![](https://koobas.hobune.stream/wayback/20220613103548im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
WWW class and HTTP Headers 3 Answers
Get information from response 0 Answers
How to load a prefab / object from a http source (e.g. REST API)? 2 Answers
IOS HTTP GET with Authorization Header not being sent 0 Answers
Firebase Cloud Functions with Unity using HTTP-callable function. Help needed. 0 Answers