gpt4 book ai didi

javascript - 如何使移动用户可点击动态电话号码

转载 作者:行者123 更新时间:2023-11-28 00:31:02 25 4
gpt4 key购买 nike

我的网站上有电话号码,这些电话号码会通过 JavaScript 自动插入,以显示默认电话号码(在主 JavaScript 中定义)或更改为可变电话号码(如果通过带有以下内容的 URL 字符串访问网页)其中的电话号码 - 例如:

http:www.website.com?tfid=8005551212 

我需要做的是让移动用户可以点击电话号码,这样无论出现哪个电话号码,他们都可以点击它,然后它就会调用该电话号码。

我知道,如果您用带有 tel: 属性的 anchor 包裹电话号码,它将启动点击通话功能,但是如何编写标签以便允许传递电话号码变量?例如:

<a href="tel:variable telephone number">variable telephone number</a>

最佳答案

你是这个意思吗?请重写您的脚本以不使用 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>';
}

关于javascript - 如何使移动用户可点击动态电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28987350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com