根据url子域替换或隐藏元素

时间:2012-12-04 17:07:01

标签: javascript jquery html url dom

尝试根据子域替换和隐藏某些元素。例如,如果网址为“www.uownrealestate.com”,则加载正常网站,但如果是“justin2.uownrealestate.com”,则隐藏包含带有“代理”文本的锚链接的元素。

$(document).ready(function() {
    var option = 'justin2';
    var full = window.location.host
    //window.location.host is subdomain.domain.com
    var parts = full.split('.')
    var subDomain = parts[0]
    var domain = parts[1]

    if (subDomain == option) {

        showDiv();
    }
});

function showDiv() {
    $("li:has(a:contains('Agents'))").hide();

}​

修改

$(document).ready(function() {
var option = 'justin2';


var full = window.location.host
if(full.indexOf(option) > -1){
showDiv(); 
}
});
function showDiv() {
$("li:has(a:contains('Agents'))").hide();

}

仍然没有发生任何事情。还有其他建议吗?此脚本也在外部js文件中。这根本不重要吗?这是li元素的一个snippit。

  <li><a href="only-at-uown.html">Only at uOwn</a>
    <ul>
<li><a href="showing-simulations.html"><span> Showing Simulations</span></a></li>
<li><a href="3d-mapping.html"><span> 3D Mapping</span></a></li>
<li><a href="solar-signs.html"><span> Solar-Powered Yard Signs</span></a></li>
<li><a href="paper-free-office.html"> Paper-Free Offices</a></li>
    <li><a href="prestige-package.html"> Prestige Package</a></li>
<li><a href="only-at-uown.html"> <em>Much more...</em></a></li>

    </ul>
   </li>


 <li><a href="realtors-agents.html">Agents</a></li>


 <li><a href="3d-showcase.html">3D Showcase</a></li>

1 个答案:

答案 0 :(得分:1)

尝试使用.indexOf(

var full = window.location.host;
if(full.indexOf(option) > -1){
   showDiv(); 
}