anonymous-dev
anonymous-dev

Reputation: 3539

How can I open a url in a new window on safari iphone

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.

Upvotes: 5

Views: 9753

Answers (6)

Jophiel0422
Jophiel0422

Reputation: 29

The only way works for me.

let wnd = window.open('about:blank', '_blank');
if(!wnd){alert("Browser block window open");return;}
wnd.location.href = url;

Upvotes: 0

ינון רחמים
ינון רחמים

Reputation: 726

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 :)

Upvotes: 1

Aidan Young
Aidan Young

Reputation: 614

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'"

Upvotes: 3

Proking73Questioneer
Proking73Questioneer

Reputation: 137

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

Upvotes: -1

Bernard
Bernard

Reputation: 36

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

Upvotes: 1

Thomas_krk
Thomas_krk

Reputation: 224

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>

Upvotes: 1

Related Questions