-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintro.js
174 lines (105 loc) · 2.49 KB
/
intro.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Basics
//// Inline
document.write('Hello World!');
//// File
// Variables
var myvar;
myvar = 5;
myvar = true;
myvar = 'hello';
document.write(myvar);
// Operators
var five = 5;
var two = 2;
var result = five + two; // => 7
// Strings
var hello = 'Hej';
var name = ' JavaScript';
document.write(hello + name);
document.write(hello.length);
document.write(hello.substring(0,3));
// Arrays
var a = new Array();
a[0] = 'Hyper';
a[1] = 'Island';
a[2] = 'codes';
document.write(a[0] + a[1] + a[2]);
document.write(a.join(' '));
a = [1, 2, 3];
// Objects
var o = new Object();
var o = {};
o['name'] = 'Johannes';
o['mode'] = 'hyper';
document.write(o['mode']);
o.name = 'Johannes';
document.write(o.name);
var o = {
name: 'Johannes',
mode: 'happy'
};
document.write(JSON.stringify(o));
// Functions
function sayHi() {
document.write('Hello');
}
sayHi();
var sayHi = function (name) {
document.write('Hello ' + name);
}
sayHi('Johannes');
sayHi('Hyper Island');
// Flow control
var a = 7;
if(a > 5) {
alert('It works!');
}
var a = 'hyper';
if(a == 'hyper') {
alert('It works!');
}
for(var i = 0; i < 5; i++) {
document.write(i + '<br>')
}
// DOM Manipulation
var heading = document.getElementById('unique-heading')
heading.innerHTML = 'this is unique'
heading.style.backgroundColor = 'tomato';
var elements = document.getElementsByClassName('hyper-headings');
var heading = elements[0];
heading.innerHTML = 'found you!'
for(var i = 0; i < elements.length; i++) {
var element = elements[i];
element.innerHTML = 'hello ' + i
}
var heading = document.createElement('h1');
heading.innerHTML = 'This is a heading';
document.body.appendChild(heading);
// Events
heading.addEventListener('click', function(event){
heading.style.background = 'turquoise';
})
// CSS transitions
// Hover button
var button = document.getElementById('suprise-button')
button.classList.add('make-it-pop')
button.addEventListener('click', function(event){
button.classList.add('make-it-pop')
})
// Local storage
var button = document.getElementById('button');
var heading = document.getElementById('heading');
var textField = document.getElementById('text-field');
var greet = function (name) {
heading.innerHTML = 'Hello ' + name + '!';
}
var name = localStorage.getItem('userName');
if(name != null) {
greet(name);
textField.value = name;
}
button.addEventListener('click', function(event){
var name = textField.value;
greet(name);
localStorage.setItem('userName', name);
})