Each HTTP Request goes through the following steps:
- Open a connection to a web server.
- Request a document.
- Wait for the server to transmit the HTML/JSON/XML/etc. document.
- After it has received the document, close the connection.
- Interpret the document.
- Load and apply external resources(CSS, JS, etc) specified by the document.
Because JavaScript isn't run until the entire document has been sent back to the browser, it may need to look for data in the document itself.
There are only a few places that data can live in:
- A tag or attribute on the document.
- The URL used to request the document.
- In a
script
tag, often used to set a global variable. - A cookie
This challenge will allow you to demonstrate your ability to place and load data from DOM elements, URLs, and script tags. Optionally, you may show off your skill with cookies.
Download this skeleton. Modify the routes, views, and javascript to provide data from the server to the client immediately on page load.
- Modify the index page so the buttons update the heading with the data provided in the buttons data-heading attribute.
- Modify the skills page so the button updates the heading with the skill in the URL.
- Modify the meals page so the button updates the heading with a random meal from the meals instance variable.
- Your code reads data from a URL in Javascript.
- Your code pulls data from an elements data attribute.
- Your code uses a global javascript object provided by the server without requiring an ajax request.
Cookies are a way to persist data across multiple requests. Provide the user with a way they can set a favorite color, then allow them to press a button on any page to change the background to their favorite color.
- Create a form which allows a user to set their favorite color. Store it in a cookie.
- Add a button to all the other pages of the application that changes the page background to the user provided favorite color by reading the cookie via javascript.
Hint: When you set a cookie on the server, you need to tell it which domains
may access the
cookie
or chrome prevents you from reading it with javascript. Make sure you set the
domain to an empty string (""
)!
Cookies are also restricted to the path they were set on unless you provide a path when you set the cookie.
- You can read a cookie with javascript on any page of the website.
- You can set a cookie with ruby.