INFO 253A: Front End Web Architecture
Kay Ashaolu
<body>
<h1>Test</h1>
<script src= "../js/script.js" type="text/javascript" ></script>
</body>
html/index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="../css/style.css" />
</head>
<body>
<div id="title" class="heading">
Info Web Arch Languages/Frameworks!
</div>
<ul>
<li class="heading">HTML/CSS/JavaScript</li>
<li>Python / Flask</li>
<li>Heroku</li>
</ul>
<!-- a comment node -->
<script src="../js/script.js"></script>
</body>
</html>
css/style.css
.heading {
color: red;
background-color: yellow;
}
js/script.js
function printBodyDOM() {
let childNodes = document.body.childNodes;
for(let i=0; i<childNodes.length; i++) {
alert(childNodes[i]);
}
}
printBodyDOM();
function printHeadingClassElements() {
let childNodes = document.querySelectorAll(".heading");
for(let i=0; i<childNodes.length; i++) {
alert(childNodes[i]);
}
}
printHeadingClassElements();
function printTitleIDElements() {
let childNodes = document.querySelectorAll("#title");
for(let i=0; i<childNodes.length; i++) {
alert(childNodes[i]);
}
}
printTitleIDElements();
function printLIElements() {
let childNodes = document.querySelectorAll("li");
for(let i=0; i<childNodes.length; i++) {
alert(childNodes[i]);
}
}
printLIElements();
These run significantly faster than querySelectorAll, and should be used when possible
function changeTitleText(titleText) {
let titleNode = document.querySelectorAll("#title")[0];
titleNode.innerHTML = titleText;
}
let titleText = prompt("Change title text");
changeTitleText(titleText);
function addClass(className) {
let ulNode = document.querySelectorAll("ul")[0];
ulNode.classList.add(className);
}
let className = prompt("Add Class");
addClass(className);
Note: to run, may need to run: "[sudo] pip install requests" to install requests package
import requests
link = "https://www.ischool.berkeley.edu/people/kay-ashaolu"
f = requests.get(link)
print(f.text)
On a user event, run this function
let titleNode = document.querySelector("#title");
titleNode.addEventListener("click", function(event) {
titleNode.classList.add("highlight");
})
html/index.html (1/2)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="../css/style.css" />
</head>
<body>
<form id="locationForm">
<label for="name">Name: </label>
<input type="text" name="name" id="name" /><br />
<label for="state">State</label>
<select name="state">
<option value="CA">CA</option>
<option value="NY">NY</option>
<option value="TX">TX</option>
</select><br />
<input type="submit" value="Save Location" />
</form>
<br />
html/index.html (2/2)
<div id="locationList">
</div>
<script src="../js/script.js"></script>
</body>
</html>
css/style.css (1/1)
.highlight {
color: red;
background-color: yellow;
}
js/script.js (1/1)
/* Populate with current location */
let locationDiv = document.getElementById("locationList");
if (localStorage.curLocation != null) {
locationDiv.innerHTML = localStorage.curLocation;
}
/* Run code on submit button push */
let locationForm = document.getElementById("locationForm");
locationForm.addEventListener("submit", function(event) {
let name = locationForm.elements.namedItem("name").value;
let state = locationForm.elements.namedItem("state").value;
localStorage.curLocation = name + ": " + state;
let locationDiv = document.getElementById("locationList");
locationDiv.innerHTML = localStorage.curLocation;
locationDiv.classList.add("highlight");
/* This stops the usual function of "submit" which is to send data
to another server */
event.preventDefault();
})
let getSummaryData = async () => {
let response = await fetch('https://raw.githubusercontent.com/UCB-INFO-FRONTEND-WEBARCH/assignment-solutions/master/solutions.json');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
} else {
return response.json();
}
}
getSummaryData().then((response) => {
/* This is where your code should be */
console.log(response);
/* End section where your code should be */
});