Practical application code analysis for passing JS parameters _ javascript skills

Source: Internet
Author: User
In the project, there is an Ajax loading area, which is a Div tag with the id of msg_box. This control link is included in a left-side navigation. When you link from other pages to this page, the JS Code becomes invalid. The reason is very simple. There is no p 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:

The 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:

The 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.

The 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:

The 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.