JavaScript DOM Flashcards
If you use parseInt on the value “345ss”, what will you get?
345 - it will ignore the string in it.
If you use parseInt on the value “345s4s”, what will you get?
345 - it will stop at the first non numerical character
If you use parseInt on the value “s345ss”, what will you get?
NaN - it will determine that the value is not a number because the first character is a char.
What month would be printed out with the following declaration?
var someDate = new Date(2015, 9, 22, 9);
October - months are 0 indexed… for some reason…
the Date object has some useful methods, what would you use to extract the full year?
dateObject.getFullYear()
If the month is set to 0 (jan), and you call someDate.setDate(32); - what is the result?
It will give you Feb, 1st. The system is smart enough to wrap the value around.
What’s an alternative to using Number on a string to convert it?
Subtract 0. This will coerce the string to a number
Why would you populate the JS Date object from serverside data, rather than relying on the JavaScript Date object?
Because you can’t be sure the client side computer is set up correctly.
Given the following code, what will be printed out?
var myarray = [1, 2, 3, 4, 5, 6, 7];
console.log(myarray.slice(1, 4));
2, 3, 4
NOTE: the indexes are zero based. And in the slice function, the first parameter is inclusive, the second is not
var myarray = [1, 2, 3, 4, 5, 6, 7];
console.log(myarray.slice(3));
4, 5, 6, 7
With only one parameter is selects and returns the array starting from the parameter (remember it’s inclusive, so it will start from the fourth postion - 0, 1, 2, 3 - which here contains 4) - and give you the remaining part of the string.
What is a significant use of the array’s join and split functions?
When using an array - it’s obviously useful to be able to iterate over it. However sending it over the wire we may want to send it as a string. We can use join and split to convert to / from a string
Is the sort method of the array object destructive?
Yes - it will modify the original array
How do you compare two string objects using the == operator?
string1.valueOf() == string2.valueOf();
Without this we would be comparing two string objects which would return false as they are indeed different objects.
In a traditional web application, where does the business logic live?
On the server.
In a traditional web application, where does the content layer live?
On the server
What is one reason to not embed javascript into the script tags of a HTML page?
Because a script error could prevent the page from loading.
What is the security reason to keep JS in separate files?
The CSP - content security policy will make sure only the code being run from separate files will be run.
What variable holds the browser name / version?
navigator. appName
navigator. appVersion
Browser sniffing is a bad way to determine funtionality - what is the recommended way?
Object detection
if (functionalityIwant) {
}
What can you use to determine whether the browser supports the currently recommended W3C DOM level?
if (document.getElementById) { // It's a modern browser }
If you need to test for older IE browsers - you can use document.all
What are the layers involved in “progressive enhancement”
- Semantically valid HTML
- CSS
- JavaScript - starts when document is loaded
- JavaScript - check that W3C DOM is available
- JavaScript - Check that desired components are avail.
What are some key things to think about with regards to JavaScript and accessibility?
- All content should be available with/without JS
- If there is content or functionality that only makes with JS, then the elements must be created by JS (i.e. no hanging buttons).
- JS functionality should be independent of user input
- If you make non interactive elements into interactive elements, there should be a fall back
- Scripts should not redirect, or submit without user interaction
What is the shortcomings of using document.write?
It only inserts a string - and not a set of nodes / attributes. It’s also difficult to use in separate files as the document only works where you put it into the HTML.
Can you retrieve elements with different class names using getElementsByClassName?
Yes - you just pass multiple class names to the function.
var elements = document.getElementsByClassName(“foo bar”);