Search code examples
javascriptandroidhtmlmozilla

Javascript Android Mozilla not opening page in a new tab


So i am trying in JS and in HTML to make a button or a tag to open new tab in Default browser Androind and it's not working

This is my html and js code:

HTML

<button  class="btn btn-success a-new-tab" data-href="{{ $url }}">My btn</button>

JS

$(document).ready(function () {
    $('.a-new-tab').on('click', function (event) {
        event.preventDefault();
        window.open($(this).attr('data-href'), '_blank');
        window.focus();
    })
});

Please help me!


Solution

  • I had a same issue on iPhone/iPad Safari browser, so the below solution worked for me. Perhaps, it might help you as well.

    HTML

    <button class="a-new-tab" data-href="https://www.google.com/">My btn</button>
    

    On button click, I'm getting the data-href and then set onclick attribute to the same button, and inside the attribute opening a new window

    $(".a-new-tab").on("click", function () {        
        var url = $(this).attr("data-href");
        $(this).attr( "onclick", "window.open('" + url + "'); return false;" );
    });