X

Useful JavaScript globals

Math.random() – generates a randon number between 0 and 1. You can multiply the number to get a number in a different range.

Math.floor(numberHere) – it would trim down the decimal part of a float and the end result is always an integer

Math.ceil(numberHere)  – it would give the integer to which a decimal is closest to; Math.ceil(4.6) would result in 5 while Math.ceil(4.4) would result in 4.

window.innerWidth – returns the inner width of a window’s content area

window.innerHeight – returns the inner height of a window’s content area

window.outerHeight – returns the height in pixels of the whole browser window; including any sidebars and browser utilities.

window.outerWidth – returns the width in pixels of the whole browser window; including any sidebars and browser utilities.

window.location – contains information about the current URL and can be used to redirect the user

window,location.href – returns the URL of the current page.

window.location.hostname –  returns the domain name of the web host

window.location.pathname – returns the path and filename of the current page

window.location.protocol – returns the web protocol used (http: or https:)

window.location.assign – loads a new document

window.location.reload() – reloads the current page

window.localStorage – a local storage where you can set data for future use in the user’s browser; you can treat it like an object to which you can assign and delete properties

document.querySelector(“.cssSelector”) – a new JavaScript addition that allows you to use CSS-like selectors without a library such as jQuery. This particular method returns the first matched element.

document.querySelectorAll(“.cssSelector”) – like document.querySelector but returns all matched elements.

window.encodeURI(“http://example.com”) – encodes a URL for different purposes such as the insertion of an URL as a GET parameter

window.decodeURI(“http://example.com”) – decodes an URL to its original state.

document.cookie – the cookie object contains variables that last for a specified duration between individual page and possibly browsing session changes.

parseInt(“12”, 10) – converts a numeric string into an integer. The second argument is the radix.

parseFloat(“40 years”) – converts a numeric string to a float.

41.toString() – casts different data types to string

Event Handlers

Event handlers allows an interaction to happen when the user performs certain activity on the given website.

document/element.onmousedown allows you to hook code that will be executed whenever the user clicks (puts the mouse button down) the left mouse button. Here is how you can use it:

document.onmousedown = function() {



      alert(1);



  }



document.querySelector(“p”).onmousedown = function() {



      alert(1);



  }

element.onmouseenter allows you to set an event listener that will be called whenever the mouse/cursor is within the area of the element.

Element.onmouseleave allows you to set an event listener that will be called when the mouse/cursor leaves the given element.

Element.onkeypress allows you to set an event listener that will be called when a keyboard key is pressed. Inside the event handler you can check which key was pressed to look and react to specific keyboard keys. You can check http://stackoverflow.com/questions/1846599/how-to-find-out-what-character-key-is-pressed to see how it is done in practice.

Ivan Dimov: Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter