The reason is very simple. There is no div tag with id msg_box In the DOM. How can this problem be solved?
Solution:
If the header file header.tpl.html is available for public use, write:
Copy codeThe Code is as follows:
<Script>
Function changMenu (index ){
If (typeof getElementById ("msg_box") = "object "){
// If the msg_box object exists, the object on the page is refreshed.
ShowMenu (index );
} Else {
// Redirect to the page refreshed using Ajax if it does not exist
Window. location = "/index.html ";
}
}
</Script>
But cannot refresh the content to the third topic. There are two solutions:
Solution 1:
Step 1:
If the header file header.tpl.html is available for public use, write:
Copy codeThe Code is as follows:
<Script>
Function changMenu (index ){
If (typeof getElementById ("msg_box") = "object "){
// If the msg_box object exists, the object on the page is refreshed.
ShowMenu (index );
} Else {
// Redirect to the page refreshed using Ajax if it does not exist
Window. location = "/index.html? Type = "+ index;
}
}
</Script>
Step 2:
Improved the showMenu function.
Copy codeThe Code is as follows:
Function showMenu (index ){
If (typeof getElementById ("msg_box") = "object "){
// If the msg_box object exists, the object on the page is refreshed.
......
} Else {
Url = window. location. href;
Reg =/^ (. *) \/index \. html \? Type \=\ d $/gi;
If (reg. test (url )){
// If the url of the parameter passing page is met. This parameter is obtained.
Index = url. substr (url. length-1 );
......
}
}
}
Solution 2:
Call the cookie function of js to pass Parameters
If the header file header.tpl.html is available for public use, write:
Copy codeThe Code is as follows:
<Script>
Function changMenu (){
Index = getCookie ("index ");
If (index = null) index = 1;
If (typeof getElementById ("msg_box") = "object "){
// If the msg_box object exists, the object on the page is refreshed.
ShowMenu (index );
} Else {
SetCookie ("index", index );
// Redirect to the page refreshed using Ajax if it does not exist
Window. location = "/index.html ";
}
}
Function setCookie (name, value ){
Var Then = new Date ()
Then. setTime (Then. getTime () + 1*3600000) // hour
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>