# angular.service.$browser

## Description

Constructor for the object exposed as $browser service.

This object has two goals:

- hide all the global state in the browser caused by the window object
- abstract away all the browser specific features and inconsistencies

For tests we provide `mock implementation` of the `$browser` service, which can be used for convenient testing of the application without the interaction with the real browser apis.

## Dependencies

- `$log`

## Methods

- ### addCss(url)

  Adds a stylesheet tag to the head.

  #### Parameters

  - `url – {string} –`

    Url to css file

- ### addJs(url, domId)

  Adds a script tag to the head.

  #### Parameters

  - `url – {string} –`

    Url to js file

  - `domId(optional) – {string} –`

    Optional id for the script tag

- ### addPollFn(fn)

  Adds a function to the list of functions that poller periodically executes, and starts polling if not started yet.

  #### Parameters

  - `fn – {function()} –`

    Poll function to add

  #### Returns

  `{function()}` –

  the added function

- ### bind()

  Register hover function to real browser

- ### cookies(name, value)

  The cookies method provides a 'private' low level access to browser cookies. It is not meant to be used directly, use the $cookie service instead.

  The return values vary depending on the arguments that the method was called with as follows:

  - cookies() -> hash of all cookies, this is NOT a copy of the internal state, so do not modify it
  - cookies(name, value) -> set name to value, if value is undefined delete the cookie
  - cookies(name) -> the same as (name, undefined) == DELETES (no one calls it right now that way)

  #### Parameters

  - `name(optional) – {string} –`

    Cookie name

  - `value(optional) – {string} –`

    Cokkie value

  #### Returns

  `{Object}` –

  Hash of all cookies (if called without any parameter)

- ### defer(fn, delay)

  Executes a fn asynchroniously via `setTimeout(fn, delay)`.

  Unlike when calling `setTimeout` directly, in test this function is mocked and instead of using `setTimeout` in tests, the fns are queued in an array, which can be programmatically flushed via `$browser.defer.flush()`.

  #### Parameters

  - `fn – {function()} –`

    A function, who's execution should be defered.

  - `delay(optional=0) – {number} –`

    of milliseconds to defer the function execution.

  #### Returns

  `{*}` –

  DeferId that can be used to cancel the task via `$browser.defer.cancel()`.

- ### getUrl()

  Get current browser's url

  #### Returns

  `{string}` –

  Browser's url

- ### hover(listener)

  Set hover listener.

  #### Parameters

  - `listener – {function(Object, boolean)} –`

    Function that will be called when a hover event occurs.

- ### notifyWhenNoOutstandingRequests(callback)

  #### Parameters

  - `callback – {function()} –`

    Function that will be called when no outstanding request

- ### onHashChange(listener)

  Detects if browser support onhashchange events and register a listener otherwise registers $browser poller. The `listener` will then get called when the hash changes.

  The listener gets called with either HashChangeEvent object or simple object that also contains `oldURL` and `newURL` properties.

  NOTE: this api is intended for use only by the $location service. Please use the `$location service` to monitor hash changes in angular apps.

  #### Parameters

  - `listener – {function(event)} –`

    Listener function to be called when url hash changes.

- ### setUrl(url)

  Sets browser's url

  #### Parameters

  - `url – {string} –`

    New url

- ### xhr(method, url, post, callback, header)

  Send ajax request

  #### Parameters

  - `method – {string} –`

    Requested method (get|post|put|delete|head|json)

  - `url – {string} –`

    Requested url

  - `post – {?string} –`

    Post data to send (null if nothing to post)

  - `callback – {function(number, string)} –`

    Function that will be called on response

  - `header(optional) – {object} –`

    additional HTTP headers to send with XHR. Standard headers are:

    - `Content-Type`: `application/x-www-form-urlencoded`
    - `Accept`: `application/json, text/plain, */*`
    - `X-Requested-With`: `XMLHttpRequest`