The reason is very simple. There is no div tag with the id msg_box in the DOM. How to solve this problem?
Solution:
Write in the header file header.tpl.html common to all pages:
<script> <br>function changMenu(index){ <br>if(typeof getElementById("msg_box") == "object"){ <br>// If the msg_box object exists, refresh the object of the page <br>showMenu(index); <br>}else{ <br>//If it does not exist, redirect to the page refreshed using Ajax <br>window.location = "/ index.html"; <br>} <br>} <br></script>
However, the project index.html has four pages of the same nature, all of which require Ajax to refresh. There is a problem. When the user clicks on the third column, although he can return to index.html, he cannot refresh the content to the third column. There are two solutions at this time:
Option 1:
Step 1:
Write in the header file header.tpl.html common to all pages:
<script> <br>function changMenu(index){ <br>if(typeof getElementById( "msg_box") == "object"){ <br>//If the msg_box object exists, refresh the object of the page <br>showMenu(index); <br>}else{ <br>//Refresh if it does not exist Direct to page refreshed using Ajax <br>window.location = "/index.html?type=" index; <br>} <br>} <br></script>
Step 2:
Improve showMenu function
function showMenu (index){
if(typeof getElementById("msg_box") == "object"){
//If the msg_box object exists, refresh the object of the page
......
}else{
url = window.location.href;
reg = /^(.*)/index.html?type=d$/gi;
if(reg.test(url)){
//If it matches the url of the parameter page. Then get the parameter
index = url.substr(url.length - 1);
......
}
}
}
Option 2:
Call the cookie function of JS to pass parameters
Write in the header file header.tpl.html common to all pages:
<script> <br>function changMenu(){ <br>index = getCookie("index"); <br>if( index == null) index = 1; <br>if(typeof getElementById("msg_box") == "object"){ <br>//If the msg_box object exists, refresh the object of the page <br>showMenu(index) ; <br>}else{ <br>setCookie("index", index); <br>//If it does not exist, redirect to the page refreshed using Ajax <br>window.location = "/index.html"; <br>} <br>} <br>function setCookie(name, value){ <br> var Then = new Date() <br> Then.setTime(Then.getTime() 1*3600000) //Hour<br>Document.cookie = name "=" value ";expires=" Then.toGMTString(); <br>} <br>function getCookie(name) <br>{ <br>var arr = document.cookie.match(new RegExp("(^| )" name "=([^;]*)(;|$)")); <br>if(arr != null) return unescape(arr[2]); return null; <br>} <br> <br></script>