5

Whenever I try to open a URL with window.open(), no window is opened. So I tried this

<input
  id="googleLink"
  onclick="window.open('https://www.google.com', '_blank');"
  style="display: none"
>

And I tried to click it with Typescript

this.googleLink = document.getElementById("googleLink") as HTMLElement;
this.googleLink.click();

And read this

window.open(url, '_blank'); not working on iMac/Safari

But none of it helps. I am not running my code in an async function. What can I do? I have an iPhone 11 and 7 and on both, it does not work. It works just fine on android and pc. I need the URL to be opened in a new window.

I need to do this with the onClick since I am using pixi js and it is a pixi container that is being clicked.

Setting the href won't help either because I want the page to open in a new tab.

anonymous-dev
  • 2,897
  • 9
  • 48
  • 112

5 Answers5

3

This should work on safari as well, but you can add a target to the link by adding target="_blank". This opens the link in a new tab and should also open in a new window on safari. Here is an example:

<a href="https://example.com" target="_blank">Link text.</a>

In your case wrap the input in a link:

 <input
  id="googleLink"
  style="display: none"
>

If you dont want to do this with html and instead an onclick function, you could also use the onclick="location.href='https://example.com'"

Spectric
  • 30,714
  • 6
  • 20
  • 43
Aidan Young
  • 554
  • 4
  • 15
  • I want to do it with the onClick functions since it is a canvas element that is being clicked. And setting the href changes does not open a new window. So I this is not the solution to my problem. – anonymous-dev Oct 17 '21 at 14:53
1

You can try the below

    <canvas id="myCanvas" width="200" height="100" style="border: 1px solid #000;" onclick="myFunction('thisURL')"></canvas>


  <script>
    function myFunction(url) {

      window.open(url, '_blank').focus();

    }
  </script>
Thomas_krk
  • 214
  • 1
  • 8
1

Have you checked if you have Block pop-up windows disabled in Safari Security settings? The same thing applies to mobile versions of Safari.

Bernard
  • 36
  • 2
  • Yes, I am aware. But that is exactly what the problem is. I need to be able to open a link in a blank tab without users having to go and change settings – anonymous-dev Oct 26 '21 at 05:25
  • Well, then that's impossible because of Safari security rules. You can only open in a new tab. – Bernard Oct 26 '21 at 11:48
-1

Try to replace async with a defer attribute in your script.

-1

I did something like that :

  const aElem = document.createElement("a");
  aElem.href = url;
  aElem.target = "_blank";
  document.body.append(aElem);
  aElem.click();
  aElem.remove();

works like a magic :)

ינון רחמים
  • 566
  • 1
  • 5
  • 12