Home >
Web Front-end >
JS Tutorial >
Sharing some small gains from using js to operate cookies_javascript skills
Sharing some small gains from using js to operate cookies_javascript skills
WBOY
Release: 2016-05-16 17:23:46
Original
1032 people have browsed it
In order to explain this issue clearly, we must start from the beginning.
First configure a parameter from the background and put it in a field. The field is called keywords. The value of this parameter is called efmis://|efmfj|username|2200|0||2014|http://10.20 .1.54:7001/cssServerportal222012/|||||02, regardless of the meaning of this value, I believe many people have encountered strings more complex than this. After the backend is configured, the frontend can display it like this: ${tag_bean.keywords}. You can be sure that no matter what the backend is configured, the frontend will be displayed as planned. The first problem arises: in the position of username, embed is the username of the currently logged in user and must be a dynamic code. Should it be written as efmis://|efmfj|${username}|2200|0||2014|http://10.20.1.54:7001/cssServerportal222012? Writing this way is different from expectations. It will be displayed unchanged and will not translate the EL expression into dynamic code. We do not consider whether we can use nesting of EL expressions for the time being. Obviously it cannot be used directly. It must be You need to process such a string.
This string is to be used as a parameter of a js method, for example:
clickClient = function(path,keywords){ //Start parsing and decomposing keywords keywords = keywords.replace("username","${user.username}"); var suffIndex=keywords.indexOf("http"); var prefix = keywords.substr(0,suffIndex-1); var suffix = keywords.substr(suffIndex-1); var preIndex=prefix.lastIndexOf("|") 1; var year = prefix. substr(preIndex); prefix = prefix.substr(0,preIndex); //End of parsing and decomposing keywords //Merge url keywords = prefix $("#year").val () suffix; clientInvoke(path,keywords); }
In this method, the final purpose is to call the clientInvoke method, and the parameters keywords passed in are changed. After certain processing, first replace user with ${user.username}. In the js code, even if it contains an EL expression, it will be dynamically parsed. This achieves the goal of dynamically calling the current username. When the year 2014 is also set to be dynamic and switchable, then the string must be decomposed into three parts:
Put the year in a select In the drop-down menu, when the clickClient method is touched, the year is taken out from the current option immediately, and then concatenated with the prefix and suffix, thus achieving the flexibility of year change.
There will be a problem at this time. After the year is switched, such as the default 2014, after switching to 2013, if you refresh the page, it will change back to the default 2014. What should I do? All variables are reloaded after refreshing, so the method of setting global variables will not work. So we have to ask, what does not change as the page refreshes and is easy for us to operate? After seeing the title of this article, I think everyone will know: cookie!
Cookies are resources saved locally and can be stored and retrieved at any time. They play a great role in remembering passwords. At this time we will use cookies to store the year in the cookie. Each time the page is loaded, determine whether the cookie exists. If it exists, take it out and put it into the select. If it does not exist, take it out from the select and store it in the cookie.
$(document).ready(function(){ if(getCookie("Year")==null){//The cookie does not exist, put it in setCookie("Year" ,$("#year").val()); }else{ //The cookie already exists, then take it out $("#year").val(getCookie("Year ")); } }); //Set cookie function setCookie(name,value) { //var Days = 30; // var exp = new Date(); //exp.setTime(exp.getTime() 365*24*60*60*1000); document.cookie = name "=" escape (value); // ";expires=" exp.toGMTString(); } //Read cookies function getCookie(name) { var arr,reg=new RegExp( "(^| )" name "=([^;]*)(;|$)"); if(arr=document.cookie.match(reg)) return unescape(arr[2]); else return null; }
Of course the cookie value will also change when switching years:
According to user requirements, be sure to make 2014 the default. After each cookie switching operation is completed, close the browser, reopen and log in to the homepage. The year will still be 2014, not the value last switched. So we don't need to set the expiration time of the cookie, we just need to let it be automatically cleared after the browser is closed.
Of course, if you expect the browser to remember cookies for a long time, set the expiration time. The comment code in setCookie is used to set the expiration time. Those who are interested can research it.
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