How to disable link to phone number when on Desktop?

后端 未结 20 953
清酒与你
清酒与你 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:02

    Just thought I would add my two-cents worth to (what is turning out to be a rather lengthy) discussion.

    I basically use the onClick event (on the link) to execute Javascript to return a boolean true or false. If the return value is true, i.e. some variable or function that tests if the device is a phone returns a value true, then the href URL is followed by the browser. If the the return value is false, then the href URL becomes, in effect, inactive. (Standard HTML behavior, way before HTML5.)

    Here is what I mean:-

    
    
    tel markup example
      
    
    
    
    Please ring (some fictitious number in Australia): +61 3 9111 2222
    
    

    Note that I also added some re-formatting of the link to make it appear to the user as if it's just ordinary text.

    Here is a gist I created.

    Just to finish this post/ answer, credit for writing succinct JavaScipt code for detecting a phone (based on the user agent and the ontouchstart event) goes to a fellow Melbournian rgb in this stackoverflow post

提交回复
热议问题