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 ?

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 ! :)

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

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=='http://unavailabedomain.com') ? 'image2' : 'default.gif');

When entering http://availbledomain.com 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. ?


'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, learning, and sharing knowledge.