Professional Documents
Culture Documents
CS193X: Web Programming Fundamentals: Spring 2017 Victoria Kirst (Vrk@stanford - Edu)
CS193X: Web Programming Fundamentals: Spring 2017 Victoria Kirst (Vrk@stanford - Edu)
Web Programming
Fundamentals
Spring 2017
Victoria Kirst
(vrk@stanford.edu)
Today's schedule
Today
- JS Events, again
- Finish Tic-Tac-Toe
- DOM revisited
- Hacks and mischief
Wednesday (probably)
- Animations
- Mobile events
Friday (probably)
- Classes and objects in JavaScript
- this keyword and bind
HW2 deadline extended
(CodePen)
Event.currentTarget vs target
Whoops, sorry!)
Multiple event listeners
(CodePen)
Event bubbling
div id="outer"
div id="inner"
div id="outer"
div id="inner"
event.addEventListener(
'click', onClick, { capture: true} );
div id="outer"
div id="inner"
event.addEventListener(
'click', onClick, { capture: true} );
div id="outer"
div id="inner"
body
div id="outer"
div id="inner"
"Capture phase"
body
div id="outer"
div id="inner"
"Target phase"
Then the browser invokes any event listener that was set on
the target itself. This is the "target phase" (w3c)
html
body
div id="outer"
div id="inner"
"Bubble phase"
body
div id="outer"
div id="inner"
stopPropagation()
html
body
div id="outer"
div id="inner"
In Practice
Do worry about:
- You do need to understand bubbling, though
- stopPropagation() also comes in handy
Case Study: Tic-Tac-Toe
Example: Tic Tac Toe
Step 2 complete
Distinguishing boxes
(CodePen)
Create a results div and add results to the div (CodePen)
Attach "data" to divs?
data-your-name="Your Value"
Data attributes in JavaScript
Property Description
<body>
<h1>My favorites</h1>
<section>
<p>Strawberries</p>
What's the parentNode of
<p>Chocolate</p> <section>?
</section>
</body>
parentNode
<body>
<h1>My favorites</h1>
The parentNode of
<section>
<section> is <body>.
<p>Strawberries</p>
<p>Chocolate</p>
</section> What are the childNodes
</body> of <section>?
childNodes
???
<body>
<h1>My favorites</h1>
<section>
Why does section
<p>Strawberries</p> have 5 children, not
<p>Chocolate</p> 2?!
</section>
</body>
TextNode
<body>
<h1>My favorites</h1>
<section>
<p>Strawberries</p>
<p>Chocolate</p>
</section>
</body>
TextNode
<body>
<h1>My favorites</h1>
<section>
<p>Strawberries</p>
<p>Chocolate</p>
</section>
</body>
DOM and Text nodes
{
"name": "Empty Chrome Extension",
"version": "1.0",
"description": "",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["page.js"]
}
],
"icons": {
"16": "pizza.png",
"48": "pizza.png",
"128": "pizza.png"
},
"manifest_version": 2
}
Example page.js
Result
https://www.quora.com/Why-is-learning-JavaScript-so-hard
Quora Chrome Extension
manifest.json
page.js
Aside: style attribute
nagScreen.style.display = 'none';
http://www.ondemandkorea.com/kpop-star-season-6-seou
l-qualifier.html
View-source, search for "adblock"...
Lol global scope
jwplayer().on('adBlock', function(event) {
jwplayer().remove();
$("#odk_player").html('<a id="adblock_signup_in_player"
href="https://www.ondemandkorea.com/odk-plus-benefits" target="_blank"
style="position: absolute;margin-top: 291px;margin-left: 262px;width:
406px;height: 38px;"></a><a href="http://www.ondemandkorea.com/blog/?
p=2085" target="_blank" style="position: absolute;margin-top: 341px;margin-
left: 497px;width: 172px;height: 20px;"></a><img
src="http://www.ondemandkorea.com/images/player_adblock_0_en.jpg">' );
});
<script>console.log('hello');</script>