-
Notifications
You must be signed in to change notification settings - Fork 0
/
samples.js
95 lines (72 loc) · 2.44 KB
/
samples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
var strEmpty = "No items have been saved or could not read storage.";
var uriList = [];
var list = document.getElementById("initMessage");
// if(!localStorage.getItem("???"))
if (uriList.length == 0) {
//array is empty
list.innerHTML = strEmpty;
}
// else {
// var newp = document.createElement("p");
// newp.innerHTML = strFull;
// list.appendChild(newp);
// }
function updateUriList() {
// arrays stuff
var uriList = ["milk", "eggs", "frosted flakes", "salami", "juice"];
var maxItems = 5;
var newURI = "cucember";
console.log("unmodified array: " + uriList);
var itemIndex = uriList.indexOf(newURI);
console.log("itemIndex= " + itemIndex);
if (itemIndex < 0) {
//not used before, add to array at beginning
var itemCount = uriList.unshift(newURI);
console.log("1");
}
else {
//already exists, move to top of list and de-duplicate
console.log("4");
uriList.splice(itemIndex, 1); //remove current instance
var itemCount = uriList.unshift(newURI); //add to front
}
while (itemCount > maxItems) {
uriList.pop();
itemCount--;
console.log("3");
}
console.log("modified array: " + uriList);
}
function setStorage() {
//storage stuff
var uriList = ["milk", "eggs", "frosted flakes", "salami", "juice"];
console.log("1: " + uriList);
localStorage.removeItem("uriList");
localStorage.setItem("uriList", JSON.stringify(uriList));
var uriList = JSON.parse(localStorage.getItem("uriList"));
console.log("2: " + uriList);
console.log(uriList[0]);
}
function writeUriList() {
//output array to html bullet list
var uriList = ["milk", "eggs", "frosted flakes", "salami", "juice"];
var list = document.getElementById("ulFeatureList");
var i = "";
clearList();
for (i = 0; i < uriList.length; i++) {
var newItem = document.createElement("LI"); // Create a <li> node
var newLink = document.createElement("A"); // Create and populate link href
newLink.setAttribute('href', uriList[i]);
newLink.setAttribute('target', '_blank');
var textNode = document.createTextNode(uriList[i]); // Create a text node for link text
newLink.appendChild(textNode); // Append the text to <a>
newItem.appendChild(newLink); // Append a to li
list.appendChild(newItem);
}
}
function clearList() {
var list = document.getElementById("ulFeatureList");
while (list.hasChildNodes()) {
list.removeChild(list.firstChild);
}
}