JavaScript Sort Dictionary. Display Cookies.

So, the last few weeks I am putting some attention towards JavaScript, as far as it is really an interesting language, making some things fun. Furthermore, thus I am preparing better for the Book Reviews from Apress.com, which have kindly provided me with 3 different JS books about a month ago. My policy there is, that man should understand  a little bit more than a beginner, when reviewing books for beginners. 🙂

So, let’s see what I have here today – the task started as really trivial and later it became interesting for me. The idea is to display the cookies in the browser in three different orders – Sorted by Value, by Key or by entry date (a.k.a.) historically. Thus, once the app (or file) is started, you may choose in which order to display, if you have already some cookies to display there:

results

In the example above, I have 4 cookies, named “Vitosh”, “Academy”,”Vitoshacademy” and “Beroe”. They are displayed historically, as the “historically” checkbox is checked. If you uncheck it, you may choose from the other two options to display the cookies.

Let’s see how the thing work – the whole app is in one file, which has one script. The script starts with window.addEventListener(“load”, init) and thus it adds functions to the button “Save”. The button “Delete Cookies” and the checkboxes, have their functions associated in the HTML code. Thus, whenever some checkbox is pressed, the function showCookies() is called and it shows the cookies based on the selected checkbuttons.

Some interesting enhancements, that you will see – the header “The list is currently ordered historically” is changing with every new way of order of the list. Plus in the code, I am using String.prototype.toProperCase, which I do not use a lot 🙂 The rest a pretty much standard functions for JS.

Thus, let’s summarize – no JS query and you have 3 ways of displaying the sorted dictionary. Pretty much that is all.

Here comes the code:

Available in GitHub as well! Enjoy it! 🙂

Tagged with: , ,