Many websites and blogs use generation technology to generate HTML static pages from web pages to facilitate search engine index ranking and reduce server burden. Static pages do bring convenience to SE, users and webmasters due to their stability and speed. But sometimes, it is necessary to remember the user's information. For example, after a user leaves a comment, the user's information needs to be remembered the next time he comes back without having to enter it again.
For users, this can improve their sense of belonging and familiarity. How to achieve this?
First, we need to assign several relevant cookie values to the client after the user submits a comment. This is very simple. Just assign the value directly on the comment submission page. The simple cookie assignment method under ASP uses the following statement:
Copy the code code as follows:
response.cookies(username)=name
response.cookies(username).expires=Date+30
Through the cookie assignment in the above asp program, we successfully wrote the user cookie information of our website on the user client. What we need to do next is how to read this cookie in the static page html and display it in front of the user. Because HTML is generated, we can no longer use the asp program to read this cookie. We need to read this cookie through js and assign it to the corresponding input value.
The code that uses js to read cookies and assign values is as follows:
Copy the code code as follows:
<script type=text/javascript>
//js gets cookies
var acookie=document.cookie.split(; );
function getck(sname)
{//Get individual cookies
for(var i=0;i<acookie.length;i++){
var arr=acookie[i].split(=);
if(sname==arr[0]){
if(arr.length>1)
return unescape(arr[1]);
else
return ;
}}
return ;
}
//Assign a value to the input in the corresponding form
document.form_name.input_name.value=getck(username);
</script>
In this way, the cookie information we have assigned can be successfully read from the client system in the static page and displayed. Isn't it very simple, haha. If you have other opinions, please discuss with me.