The practical application Code Analysis _javascript technique of JS parameter transfer

Source: Internet
Author: User
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>

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.