添加javascript变量以单击以调用链接

时间:2015-01-29 13:21:37

标签: javascript html variables mobile href

我要做的是在点击中添加一个javascript变量来调用href,这样每当电话号码从javascript更改时,点击调用href也会改变。定义变量的javascript具有默认值(例如:1-800-555-1212),但只要页面加载tfid定义不同电话号码的地方就可以更改(例如:http://www.domain.com?tfid=8663337777

以下是目前的例子:

<a href="tel:<this phone number needs to be dynamic as well>">Call this number: <script language="javascript" >
  key = getVar("keyword");
  tn = getVar("tfid");
  source = getVar("source");
  content = getVar("content");
  campaign = getVar("campaign");
  if(tn!="")
  {
  setcookie(key,tn);
  }
  getcookie(); 
  </script></a>

编辑:

为了显示电话号码显示或切换的所有元素,我在下面添加了javascript,只要页面加载就会调用:

// JavaScript Document start
//function to set cookie from the URL
function pixelfire(debug)
{
var phone_number=getVar("phone_number");
var keyword=getVar("keyword");
var source=getVar("source");
if(keyword)
{
    //document.write("set phone number "+phone_number);
    //document.write("set keyword "+keyword);
    setcookie(keyword,phone_number);
    //document.write("back from set cookie");
}
else
{
    var keyword=get_named_cookie("MM_Keyword");
    var phone_number=get_named_cookie("MM_TrackableNumber");
    //document.write("retrieved keyword "+keyword);
    //document.write("retrieved phone number "+phone_number);
    if(keyword)
    {
    }
    else
    {
        return null;
    }
}
if(debug)
{
document.write("here are cookies<BR><P>"+document.cookie);
}
var campaign=getVar("campaign");
var content=getVar("content");
//document.write("location is "+location);
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;

//document.write("url is "+ url);
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 to retrive the cookie
function getcookie(){
    //plcae your default phone number to show in case cookie is not set
    defaultphone="8005551212";
    if(document.cookie){ //check if there is a cookie set 
          index = document.cookie.indexOf("MM_TrackableNumber");

          if (index != -1){
                namestart = (document.cookie.indexOf("=", index) + 1);
                nameend = document.cookie.indexOf(";", index);
                if (nameend == -1) {nameend = document.cookie.length;}
                document.write(formatnumber(document.cookie.substring(namestart, nameend)));

          }

            else
            {
            document.write(formatnumber(defaultphone));
            }
    }
    else
    {
    document.write(formatnumber(defaultphone));
    }
}
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);        
         }

/*end*/

这听起来很简单(我知道不是),我需要<a>标签来执行此操作:<a href="tel:FILL THIS WITH EITHER THE DEFAULT NUMBER OR THE NEW NUMBER DEFINED BY THE TFID URL">

2 个答案:

答案 0 :(得分:0)

您可以尝试将电话号码存储到ID中,然后使用此脚本

<script>
  $(document).ready(function () {
       $(".clickable").onClick(function() {   
          var phone = $(this).attr('id')
       }); 
  }); 

</script>

<a class="clickable" id="{variable_phone_number}" href="...."> ... </a> 

编辑:试试这个

<a class="clickable" id="+298384858849" href="...."> ... </a>

<script type="text/javascript">

    $(document).ready(function () {

        var number = $(".clickable").attr('id')

    });

</script> 

请记住添加id和类属性

答案 1 :(得分:0)

你可能想把它包装成一个函数吗?

<a onclick="doSomething('123456789')" href="tel:<this phone number needs to be dynamic as well>">Call this number: </a>

<script language="javascript" >
function doSomething(telNumber){
  var tel = telNumber;
  key = getVar("keyword");
  tn = getVar("tfid");
  source = getVar("source");
  content = getVar("content");
  campaign = getVar("campaign");
  if(tn!="")
  {
    setcookie(key,tn);
  }
  getcookie(); 
}
</script>