


Sharing some small gains from using js to operate cookies_javascript skills
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:
The clickClient method is not the actual method to be called, it is just a transition method.
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:
Prefix: efmis://|efmfj|username|2200|0 ||
Year: 2014
Suffix: |http://10.20.1.54:7001/cssServerportal222012/||||02
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.
Year switch
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:
switchYear=function(year){
setCookie("Year",year);
}
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
