5

I am tying to open new tab using jquery. But, it's not working in safari or Mac.

My code:

<script>
    $(document).ready(function(){
        $.ajax({
            type: 'post',
            dataType: 'html',
            url:'http://localhost/test/remoteContent.html',
            async: false,
            success:function(data){
                window.open("http:google.com",'_blank');
            }

        })
    });
</script>
Sudhir
  • 835
  • 11
  • 31

2 Answers2

5

I know it's not the answer you want, but unfortunately this is how modern browser security works. Due to the way popups and other actions can be abused to do 'bad things' on web pages, things like click for File Upload form fields and window.open in other cases will only work when the user has explicitly performed an action. These are known as trusted events and cannot be spoofed by JavaScript. I have read up on this before, and you should find this answer to be very informative.

You'll have to modify your workflow so that the tab opens after the user has clicked something, or open a new tab as part of the initial operation.

BoffinBrain
  • 6,337
  • 6
  • 33
  • 59
1

I have found alternate solution for this. After ajax response create anchor tag element and trigger click event

 var a = document.createElement('a');
 a.href = 'https://google.com';
 a.setAttribute('target', '_blank');
 a.click();
Sudhir
  • 835
  • 11
  • 31