無論是前端開發還是后臺設計,很多時候開發人員都需要獲取當前或目標網址的相關信息。這個已有現成的內置對象屬性可以直接調用了(下面是獲取當前頁面的參考代碼)
復制代碼 代碼如下:
<script type="text/javascript">
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;
thisDLoc = document.location;
strwrite = " thisURL: [" + thisURL + "]<br>"
strwrite += " thisHREF: [" + thisHREF + "]<br>"
strwrite += " thisSLoc: [" + thisSLoc + "]<br>"
strwrite += " thisDLoc: [" + thisDLoc + "]<br>"
document.write( strwrite );
</script>
復制代碼 代碼如下:
function ChangeURLParm(Turl,Parm,PValue,ClearParm){
//Turl: 網址
//Parm: 參數
//PValue: 參數值
//ClearParm: 要清除的參數
var URL,Parms,ParmsArr,IsExist;
var NewURL = Turl;//window.location.href
IsExist = false;
with(Turl){
if(indexOf('?')>0){
URL = substr(0,indexOf('?'));//不包含參數
Parms = substr(indexOf('?')+1,length);//參數
}
else{
URL = Turl;
Parms = '';
}
}
if (Parms!=''){
var i;
ParmsArr = Parms.split("&");
for(i=0;i<=ParmsArr.length-1;i++){
if (String(Parm).toUpperCase()==String(ParmsArr[i].split("=")[0]).toUpperCase()){//原來有參數Parm則改變其值
ParmsArr[i] = Parm + "=" + PValue;
IsExist = true;
if (String(ClearParm) ==""){
break;
}
}
else if ( (String(ClearParm)!="") && (String(ClearParm).toUpperCase()==String(ParmsArr[i].split("=")[0])).toUpperCase() ){//去掉參數ClearParm的值
ParmsArr[i] = ClearParm + "=";
}
}
for(i=0;i<=ParmsArr.length-1;i++){
if(i==0){
Parms = ParmsArr[i];
}
else{
Parms = Parms + "&" + ParmsArr[i];
}
}
NewURL = URL + "?" + Parms;
if (!IsExist){
NewURL = NewURL + "&" + Parm + "=" + PValue;
}
}
else{
NewURL = URL + "?" + Parm + "=" + PValue;
}
return NewURL;
}
新聞熱點
疑難解答
圖片精選