简体   繁体   中英

change class of a div if domain name =

I have 2 domains going to my site but I want to change the class of a div that has an ID of #logo.

I have tried the following but no luck...

<script type="text/javascript"> 
var hasChar1 = window.location.href.indexOf('domain01') != -1;
var hasChar2 = window.location.href.indexOf('domain') != -1;
if (hasChar1)
{
   if (hasChar1)
   {document.getElementById("logo").className = "logoNZ";}
   else
   {document.getElementById("logo").className = "";}
}
else if (hasChar2)
{document.getElementById("logo").className = "";}
</script>

Why inserting the same if statement inside itself?

This should work:

document.getElementById('logo').className = (window.location.hostname.indexOf('domain') !== -1) ? 'logoNZ' : 'notLogoNZ';

Make sure you place this code at the end of your body, after the div as already been rendered!

Have you tried something like this ?

if (window.location.hostname == "domain01"){
    document.getElementById("logo").className = "logoNZ";
}
else if (window.location.hostname == "domain"){
    document.getElementById("logo").className = "";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM