2012-11-08 40 views
1

我有2個域名前往我的網站,但我想更改ID爲#logo的div的類別。如果域名更改爲div的類別=

我曾嘗試以下,但沒有運氣...

<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> 

回答

0

爲什麼插入相同的if語句裏面本身?

這應該工作:

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

確保你把這個代碼在你的身體結束,在div後作爲已經被渲染!

0

你試過類似的東西嗎?

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