How to disable link to phone number when on Desktop?

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

    I recently had this same problem. This problem is all over stackoverflow and everywhere else. How do you hide 'tel:' prefix and keep it from blowing up in regular browsers. There's no good single answer.

    I ended up doing it this way:

    first I use metalanguage to filter browser vs mobile (like php/coldfusion/perl) based on useragent string:

    regular exp match for "/Android|webOS|iPhone|iPad|BlackBerry/i",CGI.HTTP_USER_AGENT
    

    that gives me an if/else condition for desktop browser vs phone.

    Next, my href tag looks like this: 800-555-1212

    Use CSS to style the .tel class in desktop stylesheet so it doesn't look like a link to desktop browsers. the phone number can still be clicked but its not obvious, and it wont do anything:

    /* this is in default stylesheet, styles.css: */
    .tel{
        text-decoration:none;
        color: #000;
        cursor:default;
    }
    /* it should be re-styled in mobile css: */
    .tel{
        text-decoration: underline;
        color: #0000CC;
        cursor:auto;
    }
    

    Finally, I do a little jquery on the mobile links. The jQuery gets the id from the a.tel class, and inserts it into the href property, which makes it clickable for phone users.

    The whole thing looks like this:

    
    
    
    
    
    if( device is mobile ) {
    
        
        
    
        
        
    }
    
    

    Call us today at 800-555-1212!

    One caveat to this approach: id's should be unique. If you have duplicate phone numbers on a page that you want to link, change the id to name, then you use jQuery to loop through them.

提交回复
热议问题