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


    May 21, 2023
    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');
    if (multiselect) {
    fileInput.setAttribute('multiple', '');
    if (filter) {
    fileInput.setAttribute('accept', filter);
  2. forewar


    Sep 9, 2017
    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 = "";
    4. = "_blank";
    7.       alert("openLink ended");
    8. }
    Alert opens, but nothing more happens.
  3. forewar


    Sep 9, 2017
    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:

    Second, to make it work with mobile browsers you should use onpointerup instead of onmouseup (

    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
    Code (JavaScript):
    1.            document.onpointerup = function()
    2.             {
    3.              // possible way
    4.              //, "_blank");
    6.               var tganchor = document.createElement("a");
    7.               tganchor.href = url_prepared;
    8.      = "_blank";
    9.     ;
    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.