Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Opening a link in a new browser tab or window

Discussion in 'WebGL' started by nsmith1024, Apr 22, 2017.

  1. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    869
    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)

    Code (CSharp):
    1.      Application.ExternalEval("window.open(\"http://www.unity3d.com\")");
    Anybody knows a more general way that works for all the browsers?

    Thanks
     
  2. Fizzer

    Fizzer

    Joined:
    Nov 16, 2016
    Posts:
    40
  3. GilesDMiddleton

    GilesDMiddleton

    Joined:
    Aug 12, 2015
    Posts:
    91
    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).

    Code (CSharp):
    1. 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.


    Code (CSharp):
    1.        
    2. public void OpenTwitter()
    3. {
    4.     if( Application.platform==RuntimePlatform.WebGLPlayer )
    5.     {
    6.         Application.ExternalEval("window.open(\"https://www.twitter.com/GilesDMiddleton\",\"_blank\")");
    7.         return;
    8.     }
    9.     float startTime = Time.timeSinceLevelLoad;
    10.  
    11.     //open the twitter app
    12.     Application.OpenURL( "twitter:///user?screen_name=GilesDMiddleton" );
    13.  
    14.     if (Time.timeSinceLevelLoad - startTime <= 1f)
    15.     {
    16.         // typically a user takes a second to open twitter and decide what to do
    17.         // so if the function took less than a second, it's likely it failed
    18.         // try using normal URL opening
    19.         Application.OpenURL( "https://www.twitter.com/GilesDMiddleton" );
    20.     }
    21. }
    22.  
     
    Justice0Juic3 likes this.
  4. kou_yeung

    kou_yeung

    Joined:
    Mar 24, 2016
    Posts:
    28
  5. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Code (csharp):
    1.  
    2.  Application.ExternalEval("window.open(\"http://www.unity3d.com\")");
    3.  
    is apparently no longer supported?
     
    vincentneuts_unity likes this.
  6. vincentneuts_unity

    vincentneuts_unity

    Joined:
    May 9, 2018
    Posts:
    1
    Did you already find an alternative?
     
  7. DryreL

    DryreL

    Joined:
    Feb 23, 2020
    Posts:
    49
    Same problem here
     
  8. d2clon

    d2clon

    Joined:
    Aug 19, 2017
    Posts:
    19
  9. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    944
    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.
     
    Marks4 and LilGames like this.
  10. LilGames

    LilGames

    Joined:
    Mar 30, 2015
    Posts:
    520
    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?)
     
  11. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    944
    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.
     
  12. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,494
    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
    Code (CSharp):
    1. mergeInto(LibraryManager.library, {
    2.   OpenInNewTab: function (url) {
    3.     window.open(Pointer_stringify(url));
    4.   },
    5.   OpenInSameTab: function (url) {
    6.     window.location = Pointer_stringify(url);
    7.     // or window.location.replace(Pointer_stringify(url));
    8.   }
    9. });
    Then create a class somewhere like this..
    Code (CSharp):
    1. using System.Runtime.InteropServices;
    2. public class JavaScript
    3. {
    4.   [DllImport("__Internal")]
    5.   public static extern void OpenInNewTab(string url);
    6.   [DllImport("__Internal")]
    7.   public static extern void OpenInSameTab(string url);
    8. };
    And then to use it, you can do this (let's say from a button being clicked),
    Code (CSharp):
    1. void OpenURL(string url = "https://forum.unity.com")
    2. {
    3. #if UNITY_WEBGL
    4.   JavaScript.OpenInNewTab(url);
    5. #else
    6.   Application.OpenURL(url);
    7. #endif
    8. }
    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 :rolleyes:
     
    Last edited: May 23, 2021
  13. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    491
    @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 ^.
     
  14. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,015
    @jukka_j what about iOS? At least on safari it doesn't open a link a new tab.
     
  15. AytoMaximo

    AytoMaximo

    Joined:
    Aug 26, 2015
    Posts:
    76
    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.
     
    Last edited: Aug 28, 2021
  16. ravi56

    ravi56

    Joined:
    Sep 15, 2021
    Posts:
    7
    hello ,did anybody have method for open new tab in safari?
     
  17. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    491
  18. LoneGoat

    LoneGoat

    Joined:
    Dec 16, 2020
    Posts:
    21
    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.