JavaScript Cookies Cookies are small text files that sit on your hard disk. Cookies are created when you visit websites that use cookies to store information that they need (or prefer). Websites often use cookies to personalise the user experience - such as remembering your name (assuming you supplied it previously) or remembering the items in your shopping cart from previous visits.
Now check your cookies folder to see if the cookie was created. Alternatively, write code to read the cookie.
Note: If the cookie wasn't created, check the expiry date - it needs to be a date in the future.
You can update this value by using the same code with a different value. If you want to add a second value, simply use a different variable name (for example "myContents2=").
Reading Cookies in JavaScript
*document.write(document.cookie);
Deleting Cookies in JavaScript To delete a cookie, you can use the same code you used to create it but this time, set the expiry date in the past:
What is an array? Arrays are a fundamental part of most programming languages and scripting languages. Arrays are simply an ordered stack of data items with the same data type. Using arrays, you can store multiple values under a single name. Instead of using a separate variable for each item, you can use one array to hold all of them.
For example, say you have three Frequently Asked Questions that you want to store and write to the screen. You could store these in a simple variable like this:
*faqQuestion1 = "What is an array?" *faqQuestion2 = "How to create arrays in JavaScript?" *faqQuestion3 = "What are two dimensional arrays?"
Creating Arrays in JavaScript Most languages use similar syntax to create arrays. JavaScript arrays are created by first assigning an array object to a variable name...
*var array_name = new Array(number_of_elements)
then by assigning values to the array...
*array_name[0] = "Array element"
So, using our prior example, we could write:
*var faq = new Array(3) *faq[0] = "What are JavaScript arrays" *faq[1] = "How to create arrays in JavaScript?" *faq[2] = "What are two dimensional arrays?"
Displaying Array Elements
*document.write(faq[1])
Creating Two Dimensional Arrays
*var faq = new Array(3)
*for (i=0; i <3; i++) *faq[i]=new Array(3)
*faq[0][1] = "Arrays" *faq[0][2] = "What is an array?" *faq[0][3] = "An ordered stack of data"
*faq[1][1] = "Arrays" *faq[1][2] = "How to create arrays?" *faq[1][3] = "Assign variable name to array object, then assign values to the array."
*faq[2][1] = "Arrays" *faq[2][2] = "What are two dimensional arrays?" *faq[2][3] = "An ordered grid of data"
No comments:
Post a Comment