求助javascript脚本代码,根据url条件判断,控制div层的显示和隐藏

来源:百度知道 编辑:UC知道 时间:2024/06/03 11:18:49
首页和栏目页用的是相同的网页模板,为了区分,想通过javascript 实现这样的功能:
当打开首页. 把id="box1" 的DIV层自动隐藏.
当url不是首页地址的时候, 把id="box2"的DIV层隐藏.

例如.当url为http://www.abc.com 时,自动隐藏<DIV id="box1"></DIV> 标签内的页面内容.<DIV id="box2"></DIV> 正常显示.

当url为http://www.abc.com/abc等,自动隐藏<DIV id="box2"></DIV> 标签内的页面内容.<DIV id="box1"></DIV> 正常显示.
<html>
<body>
<div id="box1">
box1
</div>
<div id="box2">
box2
</div>

<script type="text/javascript">
function disPlay(){
var b1 = document.getElementById("box1");
var b2 = document.getElementById("box2");
if(window.location.href = "

function disPlay(){
var b1 = document.getElementById("box1");
var b2 = document.getElementById("box2");
if(window.location.href = "
http://www.abc.com"){
b1.style.display = "none";
b2.style.display = "block"
}
else{
b1.style.display = "block";
b2.style.display = "none"
}
}

怎么不用location.pathname

if (location.pathname.indexOf('index') != -1 || location.pathname.indexOf('default') != -1) {
b1.style.display = "none";
b2.style.display = "block"
}
else {
b1.style.display = "block";
b2.style.display = "none"
}

location.href

能得到当前url~~

不过 你是域名,所以要考虑到默认页的情况~

如果可以的话用后台语言控制