Nation JavaScript Library v2

Utils

function
Utils()

Dependencies:

None

About:

This class features a number of useful utility methods for use around your site

ajax

method
Utils.prototype.ajax()

Option name Type Description
options object

Object containing required settings. The only required option is 'url'
url {string} (Required) The URL to load
method {string: "get"} Load the URL via either get or post
success {function} A method to call when the URL has been successfully loaded
error {function} A method to call when an error occurs after attempting to load the URL
dataType {string} The type of data that will be loaded: "text", json", "jsonp", "script", "arraybuffer", "blob", or "document"
data {string/object} Data to send to the server as part of the request. Either a query string or an object of key/value pairs
id {string} A unique ID to attach to script loads. Allows author to check document if a script has already loaded before loading it.
contentType {string} Override the content type sent to the server in the request's header
progress {function} A method to call when load progress occurs. The method will receive a percentage between 0 and 1 representing progress. Only called in IE10+ and modren browsers

Load a URL
Note: If loading binary data, a handy reference: http://www.html5rocks.com/en/tutorials/file/xhr2/

setStyle

method
Utils.prototype.setStyle()

Option name Type Description
element domelement

The DOM element to apply the new styles to

styles object

An object of key-value pairs stating each new style's name and it's new value

Set one or more styles on an element

getCookie

method
Utils.prototype.getCookie()

Option name Type Description
cookieName string

The name of the cookie to find a value for

Get a cookie value by name

setCookie

method
Utils.prototype.setCookie()

Option name Type Description
cookieName string

The name of the new cookie

cookieValue string

The value the new cookie should contain

days string

Optional cookie duration, in days. If left blank, the cookie will act as a session cookie

Set a new cookie for the current domain

getPageElement

method
Utils.prototype.getPageElement() ->domelement

Get the main element that can scroll the whole page

isEventSupported

method
Utils.prototype.isEventSupported()

Option name Type Description
eventName string

The name of the event to check exists

element domelement_or_string

The element that may contain the event. If set as a string, eg. "span", the element matching the string will be created. Leave out to just test a standard div

Check if an element supports a particular event
Via Modernizer and http://perfectionkills.com/detecting-event-support-without-browser-sniffing/

serializeForm

method
Utils.prototype.serializeForm()

Option name Type Description
formElement domelement

The form element containing fields to serialise

Serialise form data

getDOMElement

method
Utils.prototype.getDOMElement()

Option name Type Description
selector domelement_or_string_or_jQueryobject

A selector string, DOM element, or jQuery object from which to return a matching DOM element

Get actual DOM element from a selector

getPrefixedName

method
Utils.prototype.getPrefixedName()

Option name Type Description
propertyName string

The name of the style that may require a prefix before it works

Return a version of the passed property name that is supported by the current browser
This will either be the one passed in, or a prefixed version of it

camelcaseString

method
Utils.prototype.camelcaseString()

Option name Type Description
value string

The string to camelcase

jsProperty boolean

True if value should be treated as a code property (remove dashes and uppercase the letter that follows one)

upperFirstChat boolean

True if the first character should always be uppercase (overrides jsProperty, when true, so that the first character is uppercase)

Convert a passed string to camelcase format