Vanessa Ferdinand - December 10, 2019
In this tutorial, we are going to make 3 webpages and run them locally on your machine.
Each webpage will have its own file structure, with the root directory at the top of that structure.
So for the sake of being organized, let's set those up first.
Alright, let's get started!
Open your favorite text editor, like Sublime Text.
Create a new file called index.html and paste this code in there.
<!DOCTYPE html>
<html>
<head>
<title>My webpage</title>
</head>
<body></body>
</html>
Now type something into the body. For example: <body>Hello world!</body>
Save index.html in the root directory hello_world_webpage.
Now open index.html with a web browser. Think to yourself: I just made a freakin webpage!
The coding language above is called HTML - it's is the main language of webpage design. Learn how to code in HTML.
To display an image on a webpage, we need to specify a path to an image file in your html.
If the file is located online, the url is the path to that file.
Add this code to index.html and then take a look at it in your browser.
<!DOCTYPE html>
<html>
<head>
<title>My webpage</title>
</head>
<body>
Hello world! <br>
<img src="http://vanessaferdinand.com/assets/world.png" width=500>
</body>
</html>
We can also create our own file system.
Here's what your file structure should look like now:
Now edit index.html to include the new path to your file:
<!DOCTYPE html>
<html>
<head>
<title>My webpage</title>
</head>
<body>
Hello world! <br>
<img src="./images/world.png" width=500>
</body>
</html>
If you used your own image file, make sure its name and file extension appears correctly in the path.
The type of path we're using here is a "relative path". Relative to what? To where your index.html file is!
./
means the current directory
../
means go up one level from the current directory
.........../
means go up ten levels from the current directory (but don't make file systems like that)
Always keep your index.html file in the root directory.
Learn more about HTML file paths.
Now we're going to make a webpage that has some Javascript code in it.
Javascript is a scripting language that allows us to create interactive webpages. This interactivity is useful for programming online experiments, where participants will interact with the experiment in complex ways.
Learn how to code in Javascript.
<!DOCTYPE html>
<html>
<head>
<title>My webpage</title>
</head>
<body></body>
<script>
var name = prompt("What is your name?");
alert("Hello " + name + "!");
</script>
</html>
This code contains a new section <script></script>
. This section contains two lines of Javascript code.
What does this Javascript code do?
It displays a prompt box to the user and whatever the user types into the prompt box is saved into a variable called name
.
Then, it creates an alert box that displays the string "Hello " + name + "!"
, which uses the content stored in the variable name
.
A typical online experiment will contain a crapload of Javascript code. In that case, it's a good idea to keep your javascript code in its own file. We can tell index.html to look for the javascript file the same way we told it to look for the image file.
var name = prompt("What is your name?");
alert("Hello " + name + "!");
<!DOCTYPE html>
<html>
<head>
<title>My webpage</title>
<script src="./js/myscript.js"></script>
</head>
<body></body>
</html>
Ok, so you could write all of your javascript code yourself, but you can also use code that other people already wrote: known as "libraries" and "plugins".
Two great javascript libraries for programming experiments are:
awesome for interactive animations and visual design - perfect for making gamified experiments
check out: how to get started with P5js, their example sketches, and Daniel Schiffman's EXCELLENT youtube channel of P5js tutorials!
has all the code you need for implementing standard psychology experiments
Now let's make a webpage that uses the P5 javascript library.
draw( )
.var col = "grey";
function setup() {
createCanvas(640, 480);
}
function draw() {
if (mouseIsPressed) {
fill(col);
} else {
fill("white");
}
ellipse(mouseX, mouseY, 80, 80);
}
<!DOCTYPE html>
<html>
<head>
<title>sweet p5.js example</title>
<script src="./js/p5.js"></script>
<script src="./js/sketch.js"></script>
</head>
<body></body>
</html>
Let's not do this in class - but here's how to make a web page using the jsPsych library, if you want to try that later.
The main file you need is jspsych.js, but there are lots of extras and its good to have them all in the filesystem for whatever experiment you're making.
<!DOCTYPE html>
<html>
<head>
<title>My webpage</title>
<script src="jspsych-6.0.5/jspsych.js"></script>
</head>
<body></body>
</html>
Now you're all set to use the jsPsych library and plugins for your own webpage!
jsPsych has two really good tutorials on their website, so I'll let them take you onward from here:
For all the well-intentioned, haptic learners out there...
Most web browsers (Chrome, Firefox, Safari, etc) have something called a Development Console that lets you view and interact with any webpage's HTML, CSS, Javascript, and filesystem. The console is useful for testing and developing your webpage. It's also useful for seeing how other web pages are built - if you see a webpage you like, open the console and check out what's going on in there.
Read more here: What are browser developer tools?
My examples below use Chrome.
The Elements tab shows the page's HTML:
The Sources tab shows the filesystem and allows you to view the contents of the files.
Here we're looking at the sketch.js file:
The Console tab contains a command line where you can directly interact with the javascript that is running on the webpage!
Type in a variable name, press enter, and you'll see its current value.
Example: Assign a new value to the variable "col" in the p5js example and play around.
When you do this, you're only changing the code that your browser downloaded to your machine from the server.
If you reload the webpage (i.e. download the website files from the server again), all your changes will be gone.
There are also tools for testing your webpage on different devices. Click this button:
The vast majority of online experiments out there are written in Javascript. This means that your participants (or a hacker) can view all of your experiment code, mess around with it, and you'd be none the wiser.
For example, if you're running an online experiment where participants get a "completion code" at the end of the experiment to prove they did it, they could open the Javascript Console, look for a variable that contains the completion code, and give you that code without ever doing the experiment.
This is something to be aware of, because you can design your code to be less hackable. An alternative is to transfer most of the heavy lifting to your experiment's backend, which is located on a server. Unfortunately, this violates an old-skool principle of web design, which is to load the client side with as much of the computing work as possible, to prevent your server from being overloaded with requests. Fortunately, we can now host our experiments on modern day cloud computing servers like Google App Engine, which are built to dynamically scale their computing resources to the needs of your incoming requests.
However, basically no one hacks online experiments right now. So you're good to go with the heavy javascript frontend. For now...
What we did in this tutorial was make three web pages that run locally on your own computer.
These web pages exist in a filesystem you made that contains code you wrote in HTML and Javascript.
When you open index.html with your web browser, it displays the web page you made.
The reason your web page is running locally is because the filesystem is stored on your computer.
If you give someone else this filesystem (like with a USB drive), they'll be able to display your webpage locally from their own computer, using their browser.
But what a tedious way to share your webpage...
The next step is putting this web page onto a server out there in internetland, so other people can request the code via a URL ...instead of asking you for it.