Home > Software design >  JavaScript window.location.href and click() methods doesn't work on mobile phone
JavaScript window.location.href and click() methods doesn't work on mobile phone

Time:04-09

I need to redirect the user to phone number. Everything is WORKS FINE ON DESKTOP but window.location.href and click() methods doesn't work on mobile phone's browser.

Code 1

        <a href="tel:555-666-7777" id="test">555-666-7777</a>
        <script>
        var element = document.getElementById('test'); 
        element.click();
        </script>

Code 2

        <script>
        window.location.href="tel:555-666-7777";
        </script>

CodePudding user response:

You must call another tab to use "tel" action, try like this:

  $(function() {
    $('.phone').click(function() {
      var PhoneNumber = $(this).text();
      PhoneNumber = PhoneNumber.replace('Phone:', '_self');
      //PhoneNumber = PhoneNumber.replace('Phone:', ''); or without _self
      window.location.href = 'tel://'   PhoneNumber; 
     // you can use  window.open instead like this: window.open('tel:900300400')
    });
  });

In HTML:

<span >Phone: 900 300 400</span>
  • Related