


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

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.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

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