Search Unity

How to open a Javascript popup window from Unity Web Player?

Discussion in 'Scripting' started by MattCoded, Mar 16, 2011.

  1. MattCoded

    MattCoded

    Joined:
    Mar 16, 2011
    Posts:
    4
    Hi All,

    I'm working on a Unity webplayer game that needs to open a Javascript popup window when the user clicks a button from inside Unity.

    I've tried a few different approaches (examples below) but they all seem to get blocked by the web browser. This is happening in Firefox, Safari and Chrome on the Mac, so it seems quite a common problem. The popup window in the final project will be for a Facebook-Connect login, but even the simple examples below just seem to get blocked.

    We made a few games before in Flash that open popup windows in the same way and don't get blocked, so I wonder if there's something extra I need to do in Unity to make sure the browser recognises the mouse 'Click Event' and allows the popup window to be displayed.

    I've tried using a GUI button with Application.ExternalEval to run some Javascript directly.
    Code (csharp):
    1. void OnGUI ()
    2. {  
    3.     if(GUILayout.Button("Google"))
    4.     {
    5.         Application.ExternalEval("window.open('http://www.google.com', 'open_window');");
    6.     }
    7. }
    I've also tried a GUI button with Application.ExternalCall to call external Javascript on the HTML page.
    Code (csharp):
    1. void OnGUI ()
    2. {  
    3.     if(GUILayout.Button("Google"))
    4.     {
    5.         Application.ExternalCall("OpenPopupWindow");
    6.     }
    7. }
    And I've also tried using the OnMouseDown and OnMouseUp event handlers from a script attached to a GUI texture.
    Code (csharp):
    1. void OnMouseDown()
    2. {
    3.     Application.ExternalCall("OpenPopupWindow");
    4. }
    Here's the external Javascript that those last two examples call when it's embedded on the web-page.
    Code (csharp):
    1. <script language="JavaScript" type="text/javascript">
    2.  
    3. function OpenPopupWindow()
    4. {
    5.     window.open('http://www.google.com', 'open_window');
    6. }
    7.  
    8. </script>
    All three examples cause the browser to block the popup window... If anyone has any suggestions about how we could get this work it would be most appreciated ;)

    Thanks everyone...
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    There is no way to open a window through the popup blocker.
    That exactly the reason the popup blocker is there to prevent any kind of unauthorized opening of newe windows / tabs

    All you can do is show an iframe / div or alternative redirect the whole page
     
  3. MattCoded

    MattCoded

    Joined:
    Mar 16, 2011
    Posts:
    4
    Hi dreamora,

    Thanks for the info, yep that makes sense :)

    In the final game, the popup window will be auto-generated by a call to the Facebook Javascript SDK, so I don't think we would be able to target an iframe or div...

    So it looks like it might be best to use a HTML button outside of the Unity webplayer instead, hopefully that would stop the popup getting blocked.

    Thanks
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Facebook stuff normally are actually overlay over the current window, facebook does not open new windows commonly.

    and the button won't help.
    the problem is not unity calling out or alike, its window.open
     
  5. LD

    LD

    Joined:
    Nov 6, 2009
    Posts:
    40
    Hi,

    I don't think that you're entirely correct dreamora. I'm currently experiencing the same problems as described by MattCoded.
    The JS below, when called from Flash, or a simple <a href="javascript:OpenWindow()">open window</a> - will open a popup window.

    When called from Flash, it has to be from a MouseEvent.CLICK event. Popup blockers will not block this (at least the default ones in Chrome, IE, and FF, perhaps more over-zealous ones will).

    It looks like the Unity web plugin somehow doesn't dispatch the same kind of event to the browser.

    Also, many Flash games with FB functionality *will* open a popup window for authentication, it's (i believe) the default behaviour with the Adobe ActionScript 3 SDK for Facebook Platform: http://code.google.com/p/facebook-actionscript-api - and I have done this for previous FB-enabled games in Flash.

    So my question: Is there any way to get Unity to open a popup window that isn't blocked? It is 100% possible in both HTML and Flash.

    FYI, code for Flash:
     
  6. vahi2008

    vahi2008

    Joined:
    Oct 1, 2010
    Posts:
    321
    very interesting topic, but have any1 got solution to this ?

    can we open iframe in unity GUI pop up ?
     
    Last edited: May 30, 2011