js2 Flashcards
Javascript: In the javascript console, to return the html of the current page that has a certain css id, type
document.getElementById(“cssid”)
Javascript: In the javascript console, to return just the first html element of the current page that has any css selector, type
document.querySelector(“.css-selector”)
javascript: To return an array of all of the html tags of the body of a page, type
document.body.children
javascript: To return an array of all of the html tags on a page with a certain class name, type
document.getElementByClassName(“class”)
javascript: To go to a different url, type
window.location.replace(“http://www.google.com”);
javascript: To return the url (including query string) of the current page, type
window.location.href
javascript: to slice a string, type
string_var.slice(1,5);
javascript: To return the index of a string, type
string_var.indexOf(“substring”);
javascript: To return the query string (including the “?”), type
window.location.search
javascript: To set a cookie on the browser, type
document.cookie=”key=a value; expires=Thu, 01 Jan 2016 12:00:00 UTC; path=/”;
javascript: To delete a cookie
update it with an expiration date that is in the past.
javascript: To return the cookies on the browser, type
document.cookie;
javascript: To change the html that is within an element, type
document.getElementById(“id”).innerHTML = “new html”
javascript: At the end of if statements. do not put
semicolon
javascript: To use two conditional statements, use
double ampersand
javascript: To write code that will only execute after a set amount of time, type
setTimeout(function(){
console.log(“string”)
}, 2000);
note: this is 2 seconds
javascript: When you use window.location.replace(“http://url.com”)
it does not let the user return to the original page
javascript: To reload a window, type
window.location.reload();
node: you can access value one in var jsonObject = {key1: “value1”} by typing
jsonObject.key1
node: To import a file in the same directory, type
var myFile = require(“./myFile”)
nore: the ./ path is mandatory
node: To expose a function to people importing your file, type
module.exports.exposedFunctionName = localFunctionName;
javascript: To create an object using a constructor function, type
function ConstructorFunc(param1) { this.attribute1 = param1; this.functionName = function() { console.log("string") } }
javascript: To instantiate an object made by a constructor function, type
var myInstance = new ConstructorFunc(“param1”)
javascript: To turn a function call into a constructor function you must
precede the function name with "new" e.g. var myInstance = new ConstructorFunc("param1", "param2")
javascript: By convention for constructor functions, people
capitalize the first letter
javascript: The DOM is
the javascript representation of the page