欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS 參數(shù)傳遞的實際應(yīng)用代碼分析

 更新時間:2009年09月13日 21:27:18   作者:  
在項目中,有一個Ajax加載的區(qū)域,是一個Div標(biāo)簽,id為msg_box,這個控制鏈接包含在一個左側(cè)的導(dǎo)航中,當(dāng)從其他頁面鏈接到這個頁面時,該JS代碼就失效了。
原因很簡單,在DOM中沒有id為msg_box的div標(biāo)簽,該怎么解決這個問題呢?
方案:
在所有頁面公用的頭部文件header.tpl.html中寫入:
復(fù)制代碼 代碼如下:

<script>
function changMenu(index){
if(typeof getElementById("msg_box") == "object"){
//如果存在msg_box對象 則刷新該頁的對象
showMenu(index);
}else{
//如果不存在 則重定向到使用Ajax刷新的頁面
window.location = "/index.html";
}
}
</script>

但是該項目index.html存在四個相同性質(zhì)的頁面,都需要Ajax來刷新,這樣就存在一個問題,當(dāng)用戶點擊第三個欄目時,雖然可以回到index.html,但是無法刷新內(nèi)容到第三個欄目。這時有兩種解決方案:
方案1:
第一步:
在所有頁面公用的頭部文件header.tpl.html中寫入:
復(fù)制代碼 代碼如下:

<script>
function changMenu(index){
if(typeof getElementById("msg_box") == "object"){
//如果存在msg_box對象 則刷新該頁的對象
showMenu(index);
}else{
//如果不存在 則重定向到使用Ajax刷新的頁面
window.location = "/index.html?type="+index;
}
}
</script>

第二步:
改進showMenu函數(shù)
復(fù)制代碼 代碼如下:

function showMenu(index){
if(typeof getElementById("msg_box") == "object"){
//如果存在msg_box對象 則刷新該頁的對象
......
}else{
url = window.location.href;
reg = /^(.*)\/index\.html\?type\=\d$/gi;
if(reg.test(url)){
//如果符合傳參數(shù)頁面的url。則獲取該參數(shù)
index = url.substr(url.length - 1);
......
}
}
}

方案2:
調(diào)用JS的cookie功能傳遞參數(shù)
在所有頁面公用的頭部文件header.tpl.html中寫入:
復(fù)制代碼 代碼如下:

<script>
function changMenu(){
index = getCookie("index");
if(index == null) index = 1;
if(typeof getElementById("msg_box") == "object"){
//如果存在msg_box對象 則刷新該頁的對象
showMenu(index);
}else{
setCookie("index", index);
//如果不存在 則重定向到使用Ajax刷新的頁面
window.location = "/index.html";
}
}
function setCookie(name, value){  
 var Then = new Date()  
 Then.setTime(Then.getTime() + 1*3600000 ) //小時  
 document.cookie = name+"="+value+";expires="+Then.toGMTString();  
}
function getCookie(name)
{
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}
 
</script>

相關(guān)文章

最新評論