Opening a link in a new browser tab or window

Hello,

In my WebGL app I need to open a link in a new window or browser tab.

I have this code but it only seems to work in FireFox, it doesnt work in Chrome (does nothing)

     Application.ExternalEval("window.open(\"http://www.unity3d.com\")");

Anybody knows a more general way that works for all the browsers?

Thanks

You’re probably getting popup blocked. Check out Popup blocker and PointerDown / PointerClick - Unity Engine - Unity Discussions

Once you’ve got passed the popup blocker (if that’s the issue)- be aware of the 2nd parameter which allows you to open in a new, or named window, so you don’t open many windows for the same content.

Replacing _blank with the name of the window you want to repeatedly target (or keep it as _blank for a new window each time).

Application.ExternalEval("window.open(\"http://www.unity3d.com\",\"_blank\")");

Note:you’ll need to call something different if you want to open a URL when not in WebGL.
And, if you’re trying to open social links things get more complicated, as you might want to call the native version.

public void OpenTwitter()
{
    if( Application.platform==RuntimePlatform.WebGLPlayer )
    {
        Application.ExternalEval("window.open(\"https://www.twitter.com/GilesDMiddleton\",\"_blank\")");
        return;
    }
    float startTime = Time.timeSinceLevelLoad;

    //open the twitter app
    Application.OpenURL( "twitter:///user?screen_name=GilesDMiddleton" );

    if (Time.timeSinceLevelLoad - startTime <= 1f)
    {
        // typically a user takes a second to open twitter and decide what to do
        // so if the function took less than a second, it's likely it failed
        // try using normal URL opening
        Application.OpenURL( "https://www.twitter.com/GilesDMiddleton" );
    }
}
1 Like

here have a great solution for popup blocker.
https://github.com/valyard/UnityWebGLOpenLink
FYR.

 Application.ExternalEval("window.open(\"http://www.unity3d.com\")");

is apparently no longer supported?

1 Like

Did you already find an alternative?

Same problem here

Application.ExternalEval is deprecated check my answer: http://answers.unity.com/answers/1817839/view.html

We are changing the behavior of the existing Application.OpenURL() function to work just like requested, i.e. to always open up the page in a new tab. This matches the expected behavior better, as navigating the current tab away does not seem to be desired practically ever. This should appear in a point release in the near future.

2 Likes

I realize that 1.5 months is short in developer time, but has this perhaps been addressed (and made it into 2018.4.x LTS?)

This fix did land, and it should be present in

2021.2.0a11
2021.1.2
2021.1.2f1
2020.3.5
2020.3.5f1
2019.4.25
2019.4.25f1

and newer. Unfortunately there is no update to Unity 2018.4.x branches as that is closed from receiving updates from the WebGL team.

Just use javascript, no issue with popup blockers or anything… add a new file to Plugins folder, for this example let’s say it’s WebGL.jslib

mergeInto(LibraryManager.library, {
  OpenInNewTab: function (url) {
    window.open(Pointer_stringify(url));
  },
  OpenInSameTab: function (url) {
    window.location = Pointer_stringify(url);
    // or window.location.replace(Pointer_stringify(url));
  }
});

Then create a class somewhere like this…

using System.Runtime.InteropServices;
public class JavaScript
{
  [DllImport("__Internal")]
  public static extern void OpenInNewTab(string url);
  [DllImport("__Internal")]
  public static extern void OpenInSameTab(string url);
};

And then to use it, you can do this (let’s say from a button being clicked),

void OpenURL(string url = "https://forum.unity.com")
{
#if UNITY_WEBGL
  JavaScript.OpenInNewTab(url);
#else
  Application.OpenURL(url);
#endif
}

In the above implementation, if the build is for WebGL, it’ll use JavaScript to open the link. For any other platforms, it’ll use Application.OpenURL.

Edit: I personally haven’t had any issue with popup blockers - though it’s not much different than OP approach aside from being via jslib, so it’s possible there still would be.

Edit 2: @jukka_j I’ll be switching to Application.OpenURL - that’s what I get for skimming before posting :roll_eyes:

@LilGames I made a free plugin that can deal with this. I didn’t test on Unity 2018.x, but it should work. It’s the same idea from what was posted above ^.

@jukka_j what about iOS? At least on safari it doesn’t open a link a new tab.

Hello! +1
I’ve managed to solve the issue with @Marks4 asset. However there is another bug: if you close the new tab and switch to the webgl app nothing is clickable anymore. But returning back by the left arrow from the new tab back to webgl works fine.

hello ,did anybody have method for open new tab in safari?

https://assetstore.unity.com/packages/tools/integration/launchurlwebgl-185980

Use:

#elif UNITY_WEBGL
// reload itch.io page
Application.ExternalEval("window.open('https://www.yourURL','_self')");
#else

Be careful about what you send as the URL, apparently there are some security problems with this method.

I’m using it to reload the itch page WebGL build and shut down the game.