JavaScript Glitch....Can anyone help?

I have an issue with a site that I tool development over. I am working on a website that is using the same WordPress install and shares the same DB, however, it was built with the intention to display 3 different phone numbers in the header area. Since the 3 sites share the same WordPress install the original developer wrote a script in the header.php that user a set of var and id to reference the number in the header so they can display 3 different numbers.

If you understand JavaScript this is very simple to read and understand. The code uses the set var to reflect the 3 numbers in the if statements and it works to a point. The website has the main header and then a sticky subheader once the user scrolls down the page, so the number goes from the main header into the sticky subheader. The problem is that instead of showing the set number the subheader displays the number I use as the reference and not the set number. So I cannot understand why it’s doing that since its using the same reference code.

I would like to keep the website private, however here is the script below that is in the header.php file.

// GET BASE URL
var base_url = window.location.hostname;
var set_phone = '(602) 457-5857';

if(base_url === "www.website1.com" || base_url === "website1.com" || base_url === "www.website2.com" || base_url === "website2.com" || base_url === "www.website3.com" || base_url === "website3")
{

if(base_url === "website1" || base_url === "website1"){
    set_phone = '(602) 900-9399';
} else if(base_url === "website2" || base_url === "website2"){
    set_phone = '(602) 714-1527';
} else if(base_url === "website3" || base_url === "website3"){
    set_phone = '(602) 293-8086';
}
document.getElementById("number_link").innerHTML = set_phone;

jQuery(document).ready(function( $ ) {

    $("#number_link").attr("href", "tel:"+set_phone);


});
 // $("#number_link").html(set_phone);

}

Easier to use an object and set the variable straight off the bat:

var domains = {
  'www.website1.com': '(602) 900-9399',
  'www.website2.com': '(602) 714-1527',
  'www.website3.com': '(602) 293-8086'
}

var hostname = window.location.hostname;
var telNo = domains[hostname];

if (hostname in domains){
  $("#number_link").attr("href", "tel:" + telNo);
} else {
  // set it to something else
}

Or something along those lines.

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.