For the simple reason that there is no div tag in the DOM with ID Msg_box, how do you solve the problem?
Scheme:
Write in header file header.tpl.html common to all pages:
Copy Code code as follows:
<script>
function Changmenu (index) {
if (typeof getElementById ("msg_box") = = "Object") {
Refreshes the object of the page if there is a Msg_box object
ShowMenu (index);
}else{
Redirect to a page that is refreshed with Ajax if it does not exist
window.location = "/index.html";
}
}
</script>
But the project index.html exist four pages of the same nature, all need Ajax to refresh, there is a problem, when the user clicks on the third column, although can return to index.html, but unable to refresh the content to the third column. There are two kinds of solutions:
Programme 1:
First step:
Write in header file header.tpl.html common to all pages:
Copy Code code as follows:
<script>
function Changmenu (index) {
if (typeof getElementById ("msg_box") = = "Object") {
Refreshes the object of the page if there is a Msg_box object
ShowMenu (index);
}else{
Redirect to a page that is refreshed with Ajax if it does not exist
window.location = "/index.html?type=" +INDEX;
}
}
</script>
Step Two:
Improved ShowMenu function
Copy Code code as follows:
function ShowMenu (index) {
if (typeof getElementById ("msg_box") = = "Object") {
Refreshes the object of the page if there is a Msg_box object
......
}else{
url = window.location.href;
Reg =/^ (. *) \/index\.html\?type\=\d$/gi;
if (reg.test (URL)) {
If the URL of the pass parameter page is met. Gets the parameter
index = URL.SUBSTR (url.length-1);
......
}
}
}
Programme 2:
Call JS cookie function pass parameter
Write in header file header.tpl.html common to all pages:
Copy Code code as follows:
<script>
Function Changmenu () {
index = GetCookie ("index");
if (index = = null) index = 1;
if (typeof getElementById ("msg_box") = = "Object") {
////If Msg_box object is present, refresh the page's Object
ShowMenu (index);
}else{
Setcookie ("index", index);
//If not present, redirect to page with Ajax refresh
Window.location = "/index.html";
}
}
function Setcookie (name, value) {
var Then = new Date ()
Then.settime (then.gettime () + 1*3600000)//hr
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);
}
</script>