CS185c
Chris Pollett
Feb 4, 2019
<a id="my-great-example" href="https://archive.org/" >The Internet Archive<a>
my_great_example = document.getElementById('my-great-example');
<!doctype html> <html> <head> <title>Class and Style Demo</title> <!-- Style tags can be used to make styles for a whole document --> <!-- This is how to write a comment in HTML --> <style> .money { color: green; } .balanced { text-align: center; } </style> </head> <body> <p>This is a normal paragraph.</p> <p class="money">We gave this paragraph a class and the document level styles applied to this class make it green.</p> <p style="font-size:20pt;">We made the text larger using an inline style directive.</p> <p class="money balanced">This paragraph belongs to two classes.</p> </body> </html>
money_elements = document.getElementsByClassName("money");
<ul data-role="listview"> <li><a href="cn_tower.html">CN Tower</a></li> <li><a href="empire_state.html">Empire State</a></li> <li><a href="sears_tower.html">Sears Tower</a></li> </ul>
We now want to talk about Javascript in detail. It will be the language used to script action in WebVR. It is also the language used to add the functionality of tags in A-Frame.
GET /185c.1.19s/ HTTP/1.1 Host:www.cs.sjsu.edu
brew install nodejs choco install nodejs sudo apt install nodejs
npm install -g live-server
live-serverthen we can point our browser to http://localhost:8000/ and see the files in that folder in a server setting as opposed to a file system setting.
Which of the following statements is true?
<!doctype html> <html> <head><title>Javascript Demo</title></head> <body> <div> <script> document.write("hi there"); </script> </div> </body> </html>
<script src="my-useful-function.js" ></script>
<a href="javascript:my_great_function()" >Click to execute</a>
return x; // has the effect of just return!
var my_variable, pi=3.14; // explicit declarations effect the scope of the variable
a++; a+=2; a--; a-=2; a = b +57;
first = "hello" second = first + "bye" // "hellobye"
a = [ "red", "green", 6 ]; //elements don't have to be same type console.log("a[2]="+a[2]); //the output of console.log can be seen using your browsers developer tools console.log(a.length); // 3, the length of the array b = [ [2,5], 7 ]; // array can be nested b[0][1] = 8; // changes values 5 to 8
a = {"bob" : 5, "sally" : {"field": 99}, "jason": [2,4,6] }; //note can nest alert(a["jason"][2]); //makes a pop up with displaying 6 alert(a.bob) // notice can use . to dereference as well a.bob = 27; //can assign value a.tony = 15 //can create new properties
for (prop in my_object) { alert("prop:" + prop + "\nvalue:" + my_object[prop]); }
function swap(i, j, a) { var tmp=a[i]; /* explicitly defined variables have scope within the function if I had declared the variable implicitly it would have global scope */ a[i] = a[j]; a[j] = tmp; }
swap(10, 5, b);
var c = swap;So could call:
c(10, 5, b);
function swap() { var i = this.arguments[0], j=this.arguments[1], a=this.arguments[2]; //same code as before }
swap = function(i, j, a) { /* same code as before*/ }
<script> function action() { var click_button = document.getElementById('click-button'); //this will get the contents of the tag as html // can also get it as objects using the children subarray var button_text = click_button.innerHTML; if ( button_text.trim() != "Click Me") { click_button.innerHTML = "Click Me"; // notice button's style attribute is also reflected as subobject click_button.style.backgroundColor = "gray"; } else { click_button.innerHTML = "Ouch"; alert(click_button.innerHTML); click_button.style.backgroundColor = "red"; } } </script> <button id="click-button" onclick="javascript:action()" > Click Me </button>
document.getElementById("foo").onchange = some_function; // or document.getElementById("foo").addEventListener("change", some_function, false); // to get rid of it we could use document.getElementById("foo").removeEventListener("change", some_function);
[My First A-Frame Example Link]
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>My First A-Frame Page</title> <script src="https://aframe.io/releases/0.8.2/aframe.min.js"></script> </head> <body> <a-scene background="color: #ECECEC"> <a-assets> <img id="my-photo" src="images/myphoto.jpg" /> </a-assets> <a-box position="-1 0.5 -3" rotation="0 45 0" src="#my-photo" shadow></a-box> <a-sphere position="0 1.25 -5" radius="1.25" color="green" shadow></a-sphere> <a-cone position="1 0.75 -3" radius="0.3" height="1.3" color="blue" shadow></a-cone> <a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#FF5566" shadow></a-plane> </a-scene> </body> </html>
<a-box color="blue" width="4"></a-box>is essentially the same as the a-entity:
<a-entity geometry="primitive: box; width: 4" material="color: blue"></a-entity>