How to disable link to phone number when on Desktop?

后端 未结 20 1037
清酒与你
清酒与你 2021-02-03 23:47

How can I add a phone number to a website that is clickable but hides the link when I\'m browsing on a website that doesn\'t support touch.

I could use Modernizr to set

20条回答
  •  春和景丽
    2021-02-04 00:05

    Here is a simple jquery-based solution which I developed to solve this problem. See code comments for explanation. https://jsfiddle.net/az96o8Ly/

    // Use event delegation, to catch clicks on links that may be added by javascript at any time.
    jQuery(document.documentElement).on('click', '[href^="tel:"]', function(e){
      try{
        // These user-agents probably support making calls natively.
        if( /Android|webOS|iPhone|iPad|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
          // Do nothing; This device probably supports making phone calls natively...
        } else {
          // Extract the phone number.
          var phoneNumber = jQuery(this).attr('href').replace('tel:', '').trim();
    
          // Tell the user to call it.
          alert("Please call "+phoneNumber);
    
          // Prevent the browser popup about unknown protocol tel:
          e.preventDefault();
          e.stopPropagation();
        }
      } catch(e){
        console.log("Exception when catching telephone call click!", e);
      }
    });
    

提交回复
热议问题