用JS取當前URL的ID值
var url = window.location.href //獲取當前URL
var param = url.split("?")[1].split("=")[1];//取得引數
if(param == 6) {
var hideEle = document.getElementById("你要隱藏的物件的id"); //獲得你要隱藏的物件
hideEle.setAttribute("className", "hide"); //隱藏
}
else {
var hideEle = document.getElementById("你要隱藏的物件的id"); //獲得你要隱藏的物件
hideEle.setAttribute("className", "show"); //顯示
}
頁面上需要定義css style:
.hide{
display: none;
}
.show{
display: block;
}
-------------------------------------------- ________________
比如 index.asp?id=xxx&name=xxx
怎麼用Javascript怎麼能取到id的name的值呢?
方法一:
<script>
function getvalue(name)
{
var str=window.location.search;
if (str.indexOf(name)!=-1)
{
var pos_start=str.indexOf(name)+name.length+1;
var pos_end=str.indexOf("&",pos_start);
if (pos_end==-1)
{
return str.substring(pos_start);
}
else
{
return str.substring(pos_start,pos_end)
}
}
else
{
return "沒有這個name值";
}
}
var strName=prompt("請輸入您所要值的名字");
alert(getvalue(strName));
</script>
方法二:
在eWebEditor線上編輯器裡是這樣處理的,給你參考一下:
var URLParams = new Array();
var aParams = document.location.search.substr(1).split('&');
for (i=0; i < aParams.length i++){
var aParam = aParams.split('=');
URLParams[aParam[0]] = aParam[1];
}
//取得傳過來的name引數
name=URLParams["name"];
方法三:
<script type="text/Javascript">
Request = {
QueryString : function(item){
var svalue = location.search.match(new RegExp("[/?/&]" + item + "=([^/&]*)(/&?)","i"));
return svalue ? svalue[1] : svalue;
}
}
alert(Request.QueryString("id"));
</script>