IDBCursor.direction

« IDBCursor

The IDBCursor.direction property is a DOMString that, on getting, returns the direction of traversal of the cursor. See the Values section below for possible values.

Syntax

 readonly    attribute IDBCursorDirection           direction;

Example

// Open our object store and then get a cursor list of all the different data items in the IDB to iterate through
    var objectStore = db.transaction('toDoList').objectStore('toDoList');
    objectStore.openCursor().onsuccess = function(event) {
      var cursor = event.target.result;
        
        // Log the current direction in which the object store is being iterated over
        console.log(cursor.direction);

        if(cursor.direction == "next") {
          // Here you could add some code to react to the direction the cursor is travelling in
        }
      
      // continue on to the record in the next cursor position
      cursor.continue();
    }

Value

A string indicating the direction in which the cursor is traversing the data:

Value Description
next This direction causes the cursor to be opened at the start of the source.
nextunique This direction causes the cursor to be opened at the start of the source. For every key with duplicate values, only the first record is yielded.
prev This direction causes the cursor to be opened at the end of the source.
prevunique This direction causes the cursor to be opened at the end of the source. For every key with duplicate values, only the first record is yielded.

Specifications

Specification Status Comment
Indexed Database API Candidate Recommendation  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 12webkit
23
4.0 (2.0) 10 17 Not supported
Feature Android Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile
Basic support 4.4 6.0 (6.0) 1.0.1 10 17 Not supported

Be careful in Chrome as it still implements the old specification along the new one. Similarly it still has the prefixed webkitIndexedDB property even if the unprefixed indexedDB is present.

See also

  • Using IndexedDB
  • The reference application for the examples in this reference: To-do Notifications (view example live.) Not every snippet appears in this example, but every example uses the same data structure and syntax, and will make sense in the context of this application.

Document Tags and Contributors

Contributors to this page: chrisdavidmills
Last updated by: chrisdavidmills,