亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

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

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

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

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

<script>
function changMenu(){
index = getCookie("index");
if(index == null) index = 1;
if(typeof getElementById("msg_box") == "object"){
//如果存在msg_box對(duì)象 則刷新該頁(yè)的對(duì)象
showMenu(index);
}else{
setCookie("index", index);
//如果不存在 則重定向到使用Ajax刷新的頁(yè)面
window.location = "/index.html";
}
}
function setCookie(name, value){  
 var Then = new Date()  
 Then.setTime(Then.getTime() + 1*3600000 ) //小時(shí)  
 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)文章

最新評(píng)論