简体   繁体   中英

window.open not working as expected on iPad Safari

I have the following code for share link feature:

  longurl = "www.google.com"
  var newWin = window.open('', share_win_name, 'width=826,height=836')
  gapi.client.load('urlshortener', 'v1', function() {
    var request = gapi.client.urlshortener.url.insert({
        'resource': {
            'longUrl': longurl
        }
    });
    var resp = request.execute(function(resp) {
      if (resp.error) {
        newWin.location = share_link + encodeURIComponent(longurl)
      } else {
        newWin.location = share_link + encodeURIComponent(resp.id)
      }
    });
  });

This code works on desktop. But on ipad safari

  • a blank new tab is opened with nothing on it
  • When I go to some other tab and come back to this tab, then I see NEW WINDOW being refreshed/reloaded

I believe its an issue as I try to first open a blank window and then update with the link information.

What is the solution?

As soon as you do the following:

newWin = window.open('', share_win_name, 'width=826,height=836');

You're creating a pop-up already.

Instead of instantiating a pop-up before, declare the newWin variable outside the scope.

Inside the callback functions, instantiate the popup.

longurl = "www.google.com"
var newWin;
gapi.client.load(...);
var resp = request.execute(function(resp) {
    if (resp.error) {
        newWin = window.open( share_link + encodeURIComponent(longurl), share_win_name, 'width=826,height=836');
    } else {
        newWin = window.open( share_link + encodeURIComponent(resp.id), share_win_name, 'width=826,height=836');
    }
  });
});

Let there be a bit of redundant code. It's better to be safe than sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM