|
方案:
在所有頁面公用的頭部文件header.tpl.html中寫入:
復制代碼 代碼如下:
<script>
function changMenu(index){
if(typeof getElementById("msg_box") == "object"){
//如果存在msg_box對象 則刷新該頁的對象
showMenu(index);
}else{
//如果不存在 則重定向到使用Ajax刷新的頁面
window.location = "/index.html";
}
}
</script>
但是該項目index.html存在四個相同性質的頁面,都需要Ajax來刷新,這樣就存在一個問題,當用戶點擊第三個欄目時,雖然可以回到index.html,但是無法刷新內容到第三個欄目。這時有兩種解決方案:
方案1:
第一步:
在所有頁面公用的頭部文件header.tpl.html中寫入:
復制代碼 代碼如下:
<script>
function changMenu(index){
if(typeof getElementById("msg_box") == "object"){
//如果存在msg_box對象 則刷新該頁的對象
showMenu(index);
}else{
//如果不存在 則重定向到使用Ajax刷新的頁面
window.location = "/index.html?type="+index;
}
}
</script>
第二步:
改進showMenu函數
復制代碼 代碼如下:
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)){
//如果符合傳參數頁面的url。則獲取該參數
index = url.substr(url.length - 1);
......
}
}
}
方案2:
調用JS的cookie功能傳遞參數
在所有頁面公用的頭部文件header.tpl.html中寫入:
復制代碼 代碼如下:
<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>
JavaScript技術:JS 參數傳遞的實際應用代碼分析,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。