JavaScript: The use of arrays and loops and the taste of cookies cookie

Source: Internet
Author: User
Tags array arrays variables
Cookie|javascript| Array | loop

The array, to put it simply, is actually a combination of several variables, and then we can call them by looping. Take a look at the following example:

<script language= "JavaScript" >
<!--Hide
var array= new Array (256, "5D multimedia", 453,999);
for (i=0;i<=3;i++)
{document.write ("+i+" element of the array is "+array[i]+" <br>);}
-->
</script>

The var array name =new Array () is the definition of the new array, the format of a variable in an array is the array name [subscript], in which case the value of array[0] is the value of 256,array[1] is "5D multimedia"; The first paragraph in the For loop I =0 is the initial value defined for I, I <=3 is the condition of the loop execution (there is a total of 4 elements, from 0 to 3), I + + represents each cycle I will add one. To execute the above code, the browser will display the following:
The No. 0 element of the array is 256
The 1th element of the array is 5D multimedia
The 2nd element of the array is 453
The 3rd element of the array is 999
As shown in the following illustration:




With the above learning, we have basically learned the language of JavaScript, and now we see that the cookies are particularly important when editing a Web page that is unique to the language in which you are writing.

Cookies are the information that records the people who visit your site, it actually resides on the user's hard disk, and even if the user has left your site, the cookie still exists on the user's hard disk, and if the user returns to your site again, the cookie will be sent back to your server. Make it easy for you to count and handle repeated visitors ' information.

Let's take a look at the source code that writes cookies:

function Setcookie ()
{var name = prompt ("Please enter your name", "");
var Cookie = "my_cookie=username&" + Escape (name);
Document. Cookie =cookie;
}

When the user enters the name, the name is placed through the escape () function into the variable of the cookie, which is then passed through document. Cookies are written to the user's hard disk.
Let's take a look at the cookie reads:

function Readcookie ()
{var Cookie = document. Cookies;
var Broken_cookie =cookie.split ("&");
var name = broken_cookie[1];
var name = unescape (name);
Alert ("Your name is:" +name);
}

Let's go through the cookie=document first. Cookies read the contents of the user's cookie, then separate the contents of the cookie from "&", then parse the name with the unescape () function, making the value of the name variable the user's name and then showing it again.

We learned the basics of JavaScript in the previous syntax. These include functions, variables, strings, mouse event handling, if statements, window actions, cookies, and so on, which may be a little difficult for beginners to be friends with. Here are a few examples to analyze the source code of various instances to drill down into the JavaScript language.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.