Index

Javascript

check if variable exists
if(typeof selenium === 'undefined') {
if(typeof selenium !== 'undefined') {
get the type of a variable
typeof [variable]
convert variable to a number
Number([variable])

regex

create regex
var regex = /.+/;
var regex = new RegExp('.+');
var regex = new RegExp('.+', 'g');
regex replace
string.replace(regex, '[replace]');
watch out: literal string replace
string.replace('find literal string', '[replacement]');
find match
var myArray = regex.exec('[input]');

examples

all input
/.+/
everything up until the last slash in string
/.*[/]/

arrays

initialize empty array
var a = [];
initialize array with values
var a = [ 0, 1, 2 ]

functions

defining a function
function foo() {
    alert('foo');
}

objects

// define object
var person = {
    variableA: "A",
    variableB: "B",
};
object with method
var person = {
    variableA: "A",
    nethodName : function() {
        return "hello " + this.variableA;
    }
};

control flow

iterate array
for(i=0; i<parent.length; i++) {
	Console.log(parent[i]);
}

dom

change window title
document.title = 'New Title';
go to url
window.location.href = 'http://www.example.com'
get full url of current page
window.location.href
get domain name only
window.location.hostname returns the domain name of the web host
get path only
window.location.pathname;
get protocol only (http|https)
window.location.protocol

Threads

invoke with a delay
setTimeout(function() { alert('foo'); }, [millis]);
delay event until user stops typing
function delay(millis, callback) {
  var timer = 0;
  return function() {
    var context = this, args = arguments;
    clearTimeout(timer);
    timer = setTimeout(function () {
      callback.apply(context, args);
    }, millis);
  };
}
example usage
document.getElementById('foo').onkeypress(delay(500, function (e) {
	alert('foo');
}));

key codes for events

https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
http://rmhh.co.uk/ascii.html
http://keycode.info/