Home >Web Front-end >JS Tutorial >Practical application code analysis of JS parameter passing_javascript skills

Practical application code analysis of JS parameter passing_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:46:45875browse

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:

Copy code The code is as follows:

<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:
Copy code The code is as follows:

<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
Copy code The code is as follows:

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:
Copy the code The code is as follows:

<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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn