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.

Question Problem with click() function in broswer scripting

Discussion in 'Web' started by RyuuyaS, Sep 6, 2023.

  1. RyuuyaS

    RyuuyaS

    Joined:
    May 21, 2023
    Posts:
    10
    Hello, I have a problem that gives me a real hard time. I have a .jslib file and it contains a code that creates an input element with a type file, then it will automatically pop up with click() function because the button the user interacts with is the Unity UI. In Chrome, it works really well but in Safari, I can't manage to get it pop up.
    This is my JS code. Does anyone know how to fix this
    fileInput = document.createElement('input');
    fileInput.setAttribute('id', gameObjectName);
    fileInput.setAttribute('type', 'file');
    fileInput.setAttribute('style','display:none;');
    fileInput.setAttribute('style','visibility:hidden;');
    if (multiselect) {
    fileInput.setAttribute('multiple', '');
    }
    if (filter) {
    fileInput.setAttribute('accept', filter);
    }
    fileInput.click();
     
  2. forewar

    forewar

    Joined:
    Sep 9, 2017
    Posts:
    6
    Hello,
    I have the same issue,

    the code doesn't work in Safari.
    Code (JavaScript):
    1. OpenLink: function() {
    2.       var anchor = document.createElement("a");
    3.       anchor.href = "https://google.com";
    4.       anchor.target = "_blank";
    5.       anchor.click();
    6.  
    7.       alert("openLink ended");
    8. }
    Alert opens, but nothing more happens.
     
  3. forewar

    forewar

    Joined:
    Sep 9, 2017
    Posts:
    6
    Okay, after hours of debugging I managed to figure it out.
    Probably my problem differs from yours, but still may be useful for somebody.

    First, the root of the problem is that some browsers (like Safari) block popups. They do it when they consider the action was programmatically triggered instead of user input.
    So the workaround is to place click handler and action as 'close' as possible.
    The solution is to bind onmouseup function, as perfectly shown in this repo: https://github.com/valyard/UnityWebGLOpenLink

    Second, to make it work with mobile browsers you should use onpointerup instead of onmouseup (https://github.com/valyard/UnityWebGLOpenLink/issues/3)

    Third, there are different ways to call the action (in my case the action was opening new url). For me I found best solution in creating element on the fly and trigger it's click() function rather than opening new window via window.open
    Code (JavaScript):
    1.            document.onpointerup = function()
    2.             {
    3.              // possible way
    4.              // window.open(url_prepared, "_blank");
    5.  
    6.               var tganchor = document.createElement("a");
    7.               tganchor.href = url_prepared;
    8.               tganchor.target = "_blank";
    9.               tganchor.click();
    10.  
    11.               document.onpointerup = null;
    12.             }
    In conclusion, the issue is obviously at the edge between Unity jslib and how browsers handle javascript.
    So for those who like me may stuck with similar issues it may be sense in searching for something like how browsers block popups.
    Hope that may help someone.
    Cheers.