Single-Page Applications (SPA). In addition, you will have the opportunity to implement this concept using one of the most popular methods of invocation,
Asynchronous Javascript and XML (AJAX). SPAs are relatively new to the web development scene. Almost all your favorite websites implement such a technique.
So what is a Single Page Application exactly?
Single Page Applications are applications built to run on a single webpage. If you want to go a little further into it, we can briefly talk about the end goal of implementing such a concept:
to provide the user with a very smooth user experience.
Creating a simple Single-Page Application
after our HTML loads.
We can choose to load our data whenever we want – when a user clicks a button, after a certain time, when something in our HTML changes, it’s up to you! I will choose to load our data when the user click a button.
[code]
<!DOCTYPE html>
<html>
<head>
<link href=“style.css” rel=“stylesheet” />
</head>
<body>
<div><h2 id=“user”>User’s name goes here</h2></div>
<button type=”button” onclick=“loadUser()”>Load User</button>
<script>
function loadUser() {
var xhttp = new XMLHttpRequest();
xhttp.open(“GET”, “http://example.com/user/user_info.txt”, true);
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById(“user”).innerHTML = xhttp.responseText;
}
}
xhttp.send();
}
</script>
</body>
</html>
[/code]
markup language.
<!DOCTYPE html> tells your browser this is an html file. Then we begin an html tag with
<html> and close it with
</html>. This is the general syntax of an HTML tag:
<tag>[content goes here]</tag>. Notice everything goes inside
<html></html>. Some tags don’t need closing tags:
<img src=“photo.jpg” /> because nothing goes in between them.
<head> tag holds information about our webpage such as our stylesheet (this is what makes webpages look pretty and interesting). We will not be working with stylesheets in this post. Information in the head tag is only for the browser and developers to work with behind the scenes. Noting in between them is printed to screen. Only information in the
<body> tag get printed to screen.
<body> is a
<div>. This is a division element. They help you group things together. Chances are any webpage you visit is rife with
<div> tags or some of the many other division elements.
<div> is an
<h2> tag. This is one of the six header tags available (h1, h2, h3,…). These will display the user’s name with big and bold lettering (by default h1 is the largest, then h2,…). I’ve given our
<h2> an id of “user”. This is like any ID card you possess. There is only one of you so there can only be one person with your ID. Likewise, there can only be one element in our HTML page with that id. This will be used to reference the
<h2> in our script.
<button> element will render as simply a button. Our button has an
attribute, onclick, whose value, “loadUser()”, corresponds to a function we created in the
<script> tag.
<script> tag holds code that either manipulates data in our HTML our retrieves information from an outside source. Our script does both.
- function loadUser() – First we define a function called loadUser. Remember our button calls this function when it is clicked.
- var xhttp = new
XMLHttpRequest() – Here we create a new instance of XMLHttpRequest which provides a way to retrieve data from a URL without having to refresh the whole page and we begin with - xhttp.open(“GET”, “http://
example.com/user/user_info.txt “, true) – This says start a GET request (hey browser get this for me) which will retrieve user_info.txt from the specified URL and set asynchrony to true. Setting this to true will allow our script to do other things while waiting for the file to be loaded. This is a very important aspect of Single-Page Application programming. You don’t want to hang your application for what could be a long time (for a big file or slow server for example). - XMLHttpRequest goes through states when it is working. We want to do something when it is in the finished or DONE state. We do this by setting xhttp.onreadystatechange to an anonymous function (a function with no name).
- if (xhttp.readyState == 4 &&
xhttp.status == 200) – In the function we check if the state of the request is one that of the DONE state. The DONE state is represented in Javascript by the number 4. We also make sure the status of the request is a successful one. There are many codes that represent different outcomes of a request’s adventure to and from a server – 200 signifies success. - document.getElementById(“user”
).innerHTML = xhttp. – document represents our HTML document. We use a method in Javascript, getElementById(“responseText user”) , to reference our <h2> which, if you remember has an id of “user”. We then access the inner content of the <h2> with innerHTML. With this, we can set the inner content to whatever we want. In our case, we want to set it to the information retrieved by the XMLHttpRequest instance. That information is stored in xhttp.responseText. - Finally, we send the request with xhttp.send() and the magic begins.
loadUser(), will execute and the contents of our
<h2> element will change to read “James Bond”.
[content_band inner_container=”true” padding_top=”10px” padding_bottom=”10px” border=”none” bg_pattern=”http://www.zipcodewilmington.com/wp-content/uploads/Triangles__1441830476_50.73.209.90-compressor.jpg”]
Want to learn more about AJAX? Check out more about our curriculum and apply today!
[/content_band]