<div id="div1"> </div>
var myDiv = document.getElementById("div1");
object.propertyHere we look into the innerHTML property of an HTML object. innerHTML represents the HTML code inside the HTML element.
HTMLobject.innerHTMLIf we were to use the DOM object myDiv from the previous code snippet,
myDiv.innerHTMLrepresents the HTML code inside the div.
<!DOCTYPE html> <html> <head> </head> <body> <div id="div1"> <p>Here is some original text</p> <p>Here are some more</p> </div> <script> var myDiv = document.getElementById("div1"); // select the DOM object that has the ID "div1" and put it into the variable myDiv alert(myDiv.innerHTML); // print out the HTML content inside myDiv myDiv.innerHTML = "<p>Here is new text</p>" // write into myDiv's HTML content </script> </body> </html>[sample code]
myDiv.styleThe above gives you access to the CSS properties of the HTML element myDiv
<!DOCTYPE html> <html> <head> </head> <body> <div id="div1" style="width:200px"> <p>Here is some original text</p> </div> <script> var myDiv = document.getElementById("div1"); // select the DOM object that has the ID "div1" and put it into the variable myDiv alert(myDiv.style.width); // read the width property of the CSS style for the myDiv DOM object myDiv.style.width = "400px"; // write (set) the width property of the CSS style for the myDiv DOM object myDiv.style.color = "red"; // write (set) the color property of the CSS style for the myDiv DOM object myDiv.style.backgroundColor = "yellow"; // write (set) the backgroundColor property of the CSS style for the myDiv DOM object </script> </body> </html>[sample code]
CSS JavaScript background-color ---> backgroundColor font-size ---> fontSize text-align ---> textAlign
var mr = document.getElementById("myslider"); alert(mr.value); my.value = 0;
<!DOCTYPE html> <html> <head> <title> The Title of this HTML Page </title> <style> body { margin: 1em; } div { border: 1px dashed black; width: 50%; height: 100px; } </style> <script> function sliderchange(){ var r = document.getElementById('myslider'); // alert(r.value); // r.value = 0; var d = document.getElementById('mydiv'); d.innerHTML = "slider value is " + r.value; } </script> </head> <body> <input type="range" min="0" max="255" value="128" onchange="sliderchange();" style="width:50%;" id="myslider" > <br><br> input monitor:<br> <div id="mydiv" style="font-size:20px;"> </div> </body> </html>
alert(); prompt(); comfirm();
history.length // how many pages the user has visited history.forward() // equivalent to the forward button history.back() // equivalent to the back button history.go() // takes an argument and takes the user to the relative place in the history list history.go(-3); // go back 3 pages history.go(2); // go forward 2 pages history.go("example.com") // go to the nearest URL in the history list that contains 'example.com'
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> The Title of this HTML Page </title> <script> function go_forward(){ history.forward(); } function go_back(){ history.back(); } function go_backtwo(){ history.go(-2); } </script> </head> <body> <script> alert("you've visited " + history.length + " web pages in this browser session"); </script> <input type="button" onclick="go_forward();" value="forward"> <input type="button" onclick="go_back();" value="back"> <input type="button" onclick="go_backtwo();" value="back two pages"> </body> </html>
location.href // 'https://www.reed.edu/art/studio-art.html' location.protocol // 'https:' location.host // 'www.reed.edu' location.pathname // '/art/studio-art.html' location.reload() // equivalent to reload buttonlocation.href can be read to gain information. You can also write into it to move to a different page
location.href = "http://www.reed.edu"; // transport the user to a new page
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> The Title of this HTML Page </title> <script> function go_reed(){ alert("you are at " + location.href); location.href = "http://www.reed.edu"; } </script> </head> <body> <input type="button" onclick="go_reed();" value="go reed"> </body> </html>
var mydate = new Date();Once you have a date object, you can get information about the date/time the moment the object was created.
var year = mydate.getFullYear(); // four-digit year var month = mydate.getMonth(); // month in number 0 - 11 var date = mydate.getDate(); // day of month var day = mydate.getDay(); // day in number 0 - 6 var hours = mydate.getHours(); // hours 0 - 23 var minutes = mydate.getMinutes(); // 0-59 var seconds = mydate.getSeconds(); // 0-59 var millseconds = mydate.getTime(); // milliseconds since Jan 1st 1970
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> The Title of this HTML Page </title> </head> <body> <script> var mydate = new Date(); var year = mydate.getFullYear(); // four-digit year var month = mydate.getMonth(); // month in number 0 - 11 var date = mydate.getDate(); // day of month document.write("today is " + (month + 1) + "/" + date + ", " + year); </script> </body> </html>
var mydate = new Date(); var mydate2 = new Date(2020, 0, 1); // Jan 1st 2020 if(mydate.getTime() > mydate2.getTime()) { alert("current time after Jan 1st, 2020"); }
Math.ceil(n) // returns n rounded up to whole number Math.floor(n) // returns n rounded down to whole number Math.max(a,b,c,...) // returns the largest number Math.min(a,b,c,...) // returns the smallest number Math.random() // returns random number between 0 and 1 Math.round(n) // returns n rounded up or down to the nearest whole numberThere are many more methods. See w3school for more info.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title> The Title of this HTML Page </title> <script> function myRand(range){ return Math.round(Math.random() * range); } </script> </head> <body> <script> alert("a random number from 0 - 100:" + myRand(100)); </script> </body> </html>