如何使移动用户可以点击动态 phone 号码

How to make dynamic phone number clickable for mobile users

我的网站上有 phone 号码,这些号码通过 JavaScript 自动插入以显示默认 phone 号码(在主要 JavaScript 中定义)或者更改为变量 phone 数字,如果网页是通过 URL 字符串访问的,其中包含 phone 数字 - 例如:

http:www.website.com?tfid=8005551212 

我需要做的是让移动用户可以点击 phone 号码,这样无论出现哪个 phone 号码,他们都可以点击它,它会呼叫 phone 号码。

我知道如果你用带有 tel: 属性的锚点包裹 phone 号码,它将启动点击通话功能,但如何编写标签才能允许phone 要传递的数字变量?例如:

可变长焦phone数字

你是这个意思吗? 请重写您的脚本以不使用 document.write。如果您在页面加载后调用其中一个函数,它会擦除​​页面。

document.getElementById("phonenumber").innerHTML='<a href="tel:'+phone_number+'">'+phone_number+'</a>';

像这样,使用你的原始代码

<div id="phonenumber">Call:  </div>

function getPhone() {
   var phone = get_named_cookie("MM_TrackableNumber");
   if (phone == null) phone = "8885551313";
   return formatnumber(phone);
}

window.onload=function() {
  document.getElementById("phonenumber").innerHTML+=getPhone();
}                            

完整代码

   function pixelfire(debug) {
       var phone_number = getVar("phone_number");
       var keyword = getVar("keyword");
       var source = getVar("source");
       if (keyword) {
           setcookie(keyword, phone_number);
       } else {
           var keyword = get_named_cookie("MM_Keyword");
           var phone_number = get_named_cookie("MM_TrackableNumber");
           return keyword || null;
       }
       var campaign = getVar("campaign");
       var content = getVar("content");
       var url = "http://www.mongoosemetrics.com/pixelfire.php?phone_number=" + phone_number;
       var url = url + "&keyword=" + keyword;
       var url = url + "&source=" + source;
       var url = url + "&campaign=" + campaign;
       var url = url + "&content=" + content;
       myImage = new Image();
       myImage.src = url;
   }

   function setcookie(key, tn, path) {
       index = -1;
       var today = new Date();
       today.setTime(today.getTime());
       var cookie_expire_date = new Date(today.getTime() + (365 * 86400000));
       document.cookie = "MM_TrackableNumber=" + tn + ";path=/;expires=" + cookie_expire_date.toGMTString();
       document.cookie = "MM_Keyword=" + key + ";path=/;expires=" + cookie_expire_date.toGMTString();
   }
   function getPhone() {
       var phone = get_named_cookie("MM_TrackableNumber");
       if (phone == null) phone = "8885551313";
       return formatnumber(phone);
   }

   function get_named_cookie(name) {
       if (document.cookie) {
           index = document.cookie.indexOf(name);
           if (index != -1) {
               namestart = (document.cookie.indexOf("=", index) + 1);
               nameend = document.cookie.indexOf(";", index);
               if (nameend == -1) {
                   nameend = document.cookie.length;
               }
               var ret_one = document.cookie.substring(namestart, nameend);
               return ret_one;
           }
       }
   }
   //function to format the phonenumber to (123) 456-7890
   function formatnumber(num) {
       _return = "1-";
       var ini = num.substring(0, 3);
       _return += ini + "-";
       var st = num.substring(3, 6);
       _return += st + "-";
       var end = num.substring(6, 10);
       _return += end;
       return _return;
   }

   function getVar(name) {
       get_string = document.location.search;
       return_value = '';

       do { //This loop is made to catch all instances of any get variable.
           name_index = get_string.indexOf(name + '=');

           if (name_index != -1) {
               get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);

               end_of_value = get_string.indexOf('&');
               if (end_of_value != -1) value = get_string.substr(0, end_of_value);
               else value = get_string;

               if (return_value == '' || value == '') return_value += value;
               else return_value += ', ' + value;
           }
       } while (name_index != -1)

       //Restores all the blank spaces.
       space = return_value.indexOf('+');
       while (space != -1) {
           return_value = return_value.substr(0, space) + ' ' + return_value.substr(space + 1, return_value.length);

           space = return_value.indexOf('+');
       }

       return (return_value);
   }
   window.onload = function () {
       key = getVar("keyword");
       tn = getVar("tfid");
       source = getVar("source");
       content = getVar("content");
       campaign = getVar("campaign");
       if (tn != "") {
           setcookie(key, tn);
       }
       var phone_number = getPhone();
       document.getElementById("phonenumber").innerHTML+='<a href="tel:'+phone_number+'">'+phone_number+'</a>';
   }