Hello,

I've been searching around internet for quite a few hours, but couldn't find a solution that would work for me.

Site has 2 domains. I would like to do a logo image swap, based on domain that the site is accessed from.

domain1 = image1
domain2 = image2

I did found some php solutions, but I can't really use php in this case, site has idiotic coding.

Are there javascript or any other alternatives how to do this ?

Dani AI

Generated

Nice thread. Building on and : use location.hostname (not the full URL) and normalize it so www. and case do not trip you up. Also plan for subdomains and a safe default. This keeps the logic clear and avoids long ternaries.

// Put this right after the <img id="myLogo"> in the markup
(function () {
  var host = (location.hostname || "").toLowerCase().replace(/^www\./, "");
  var logos = {
    "domain1.com": "/images/logo-domain1.png",
    "domain2.com": "/images/logo-domain2.png"
  };
  var img = document.getElementById("myLogo");
  if (img) img.src = logos[host] || "/images/logo-default.png";
})();

Tips:

  • Testing: do not compare against strings with "http://" (as noted). hostname is just the domain, so availabledomain.com will match, but http://availabledomain.com will not.
  • With/without www: the replace(/^www\./,"") handles both www.example.com and example.com.
  • Avoid flicker: place the script immediately after the logo <img> so it runs as soon as the element exists, or start the <img> with empty src and set a fixed width/height to prevent layout shift before JS swaps the file.

If you can touch the server, an even cleaner approach is zero-JS: map one logical path (e.g., /assets/logo.png) to a domain-specific file based on the Host header. That removes any flash and keeps templates identical. For Magento specifically (hi ), the best practice is per-website/store configuration to point each site at its own logo in the admin/theme, rather than swapping with front-end JS.

Recommended Answers

All 10 Replies

One solution is you can check it's url.

if (window.location.href.indexOf('url') >= 0) {
  //change logo for domain name with 'url'
} else {
  // change logo for the other domain
}

or you can check it using this method

if (document.domain === 'domain') {
    //change logo for domain name with 'url'
} else {
    // change logo for the other domain
}

I think the last one is better

I would have more confidence in traditional location.hostname than document.domain, which I've not encountered before.

In plain javascript :

document.getElementById('myLogo').src = (location.hostname=='domain1') ? 'image1.gif' : (location.hostname=='domain2') ? 'image2' : 'default.gif';

or in jQuery :

$('#myLogo').attr('src', (location.hostname=='domain1') ? 'image1.gif' : (location.hostname=='domain2') ? 'image2' : 'default.gif');

or the equivalent if ... else if ... or switch ... case forms.

Thank you for replying. I was plesantly surprised when I saw 2 solutions ! :)

lambing,
would I just have to add my html in place of "//change logo for domain name with 'url'" ?

Airshow,
I already tried your jQuery solution it works but I'm experiencing strange occurence.

Since I don't have second domain set up I just tried using

('#myLogo').attr('src', (location.hostname=='http://availabledomain.com') ? 'logo.png' : (location.hostname=='') ? 'image2' : 'default.gif');

When entering script would change image to default.gif instead of logo.png.

I changed default.gif o logo.png and it worked, but I wanted to find out why is this happening ?

P.S Would these scripts work with and without www. ?

Wonderland,

'http://' is not included in the string returned by location.hostname.

Try :

('#myLogo').attr('src', (location.hostname=='availabledomain.com') ? 'logo1.png' : (location.hostname=='unavailabedomain.com') ? 'logo2.png' : 'default.png');

FYI, some 8 parts of the current page's URL are parsed out and made available as properties the location object.

'http' (or 'https') appears as location.protocol.

I removed http and everything works great now, script uses correct image.

Thank you very much!

Hi Airshow

I was searching it for many weeks
I am working on Magento Store, i have 2 domain i want to show different logo on each domain.
In header file, logo is shown by this code

      <?php if ($this->getIsHomePage()):?>
        <h1 class="logo"><strong><?php echo $this->getLogoAlt() ?></strong><a href="<?php echo $this->getUrl('') ?>" title="<?php echo $this->getLogoAlt() ?>" class="logo"><img src="<?php echo $this->getLogoSrc() ?>" alt="<?php echo $this->getLogoAlt() ?>" /></a></h1>
        <?php else:?>
        <a href="<?php echo $this->getUrl('') ?>" title="<?php echo $this->getLogoAlt() ?>" class="logo"><strong><?php echo $this->getLogoAlt() ?></strong><img src="<?php echo $this->getLogoSrc() ?>" alt="<?php echo $this->getLogoAlt() ?>" /></a>
        <?php endif?>

I dont know how to adjust your code into this existing code. can you please guide me here

Waqas, that's a server-side problem with a different solution and best addressed in the PHP section, not JavaScript / DHTML / AJAX.

Did you mean i cant use JS in php based website? or you are saying that php solution is better?

Neither, I'm saying you need to ask your question in the right place in Daniweb - otherwise you won't attract the right experts.

whoa guys you give me a lot of information,I really thankful to all of you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.