All Projects → uzairfarooq → Arrive

uzairfarooq / Arrive

Licence: mit
Watch for DOM elements creation and removal

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Arrive

Dna.js
🧬 An uncomplicated user interface library for building data-driven semantic templates
Stars: ✭ 114 (-83.78%)
Mutual labels:  jquery, dom
Bigtube
YouTube player is too small to watch people writing code!
Stars: ✭ 94 (-86.63%)
Mutual labels:  watch, jquery
Femtojs
femtoJS - Really small JavaScript (ES6) library for DOM manipulation.
Stars: ✭ 122 (-82.65%)
Mutual labels:  jquery, dom
Vent
jQuery inspired DOM events library
Stars: ✭ 30 (-95.73%)
Mutual labels:  jquery, dom
Js Tracker
A chrome extension tracks front-end JavaScript that uses DOM / jQuery APIs to manipulate html dom elements (e.g., change style, attach event listener) at runtime.
Stars: ✭ 387 (-44.95%)
Mutual labels:  jquery, dom
Domquery
PHP library for easy 'jQuery like' DOM traversing and manipulation.
Stars: ✭ 84 (-88.05%)
Mutual labels:  jquery, dom
Phantomas
Headless Chromium-based web performance metrics collector and monitoring tool
Stars: ✭ 2,191 (+211.66%)
Mutual labels:  jquery, dom
Nito
A jQuery library for building user interfaces
Stars: ✭ 56 (-92.03%)
Mutual labels:  jquery, dom
Ffdynamic
Library with dynamic audio/video composition and runtime control
Stars: ✭ 274 (-61.02%)
Mutual labels:  detect, dynamic
angular-expression-parser
This library helps in achieving AngularJs equivalents of $parse, $eval and $watch in Angular.
Stars: ✭ 17 (-97.58%)
Mutual labels:  dynamic, watch
Domtastic
Small, fast, and modular DOM and event library for modern browsers.
Stars: ✭ 763 (+8.53%)
Mutual labels:  jquery, dom
Cheerio
Fast, flexible, and lean implementation of core jQuery designed specifically for the server.
Stars: ✭ 24,616 (+3401.56%)
Mutual labels:  jquery, dom
Jquery Xpath
jQuery XPath plugin (with full XPath 2.0 language support)
Stars: ✭ 173 (-75.39%)
Mutual labels:  jquery, dom
Apple Device Model List
All Apple devices model name list. 通过内部编号判断 iOS 设备型号。
Stars: ✭ 149 (-78.81%)
Mutual labels:  watch, detect
Shoestring
A lightweight, simple DOM utility made to run on a tight budget.
Stars: ✭ 447 (-36.42%)
Mutual labels:  jquery, dom
Cash
An absurdly small jQuery alternative for modern browsers.
Stars: ✭ 5,714 (+712.8%)
Mutual labels:  jquery, dom
Isinviewport
An ultra-light jQuery plugin that tells you if an element is in the viewport but with a twist.
Stars: ✭ 646 (-8.11%)
Mutual labels:  jquery
Abp Asp.net Boilerplate Project Cms
ABP module-zero +AdminLTE+Bootstrap Table+jQuery+Redis + sql server+quartz+hangfire权限管理系统
Stars: ✭ 677 (-3.7%)
Mutual labels:  jquery
Leafpub
Simple, beautiful, open source publishing.
Stars: ✭ 645 (-8.25%)
Mutual labels:  jquery
Ramjet
Morph DOM elements from one state to another with smooth animations and transitions
Stars: ✭ 5,455 (+675.96%)
Mutual labels:  dom

arrive.js

CDNJS version

arrive.js provides events to watch for DOM elements creation and removal. It makes use of Mutation Observers internally.

Download arrive.min.js (latest)

or use Bower to install:

# install arrive.js and add it to bower.json dependencies
$ bower install arrive --save
Node.js / NPM

Node.js users can install using npm:

$ npm install arrive --save

Usage

The library does not depend on jQuery, you can replace jQuery elements in the examples below with pure javascript elements and it would work fine.

Watch for elements creation

Use arrive event to watch for elements creation:

// watch for creation of an element which satisfies the selector ".test-elem"
$(document).arrive(".test-elem", function() {
    // 'this' refers to the newly created element
    var $newElem = $(this);
});

// the above event would watch for creation of element in whole document
// it's better to be more specific whenever possible, for example
$(".container-1").arrive(".test-elem", function() {
    var $newElem = $(this);
});

// as of v2.3.2, new element is also passed as argument to the callback function.
// This is to support arrow functions as 'this' is not bindable in arrow functions.
$(document).arrive(".test-elem", function(newElem) {
    var $newElem = $(newElem);
});

In pure javascript you can call the function on document, window, any HTMLElement or NodeList, like this:

// watch for element creation in the whole HTML document
document.arrive(".test-elem", function() {
    // 'this' refers to the newly created element
});

// this will attach arrive event to all elements in the NodeList
document.getElementsByClassName(".container-1").arrive(".test-elem", function() {
    // 'this' refers to the newly created element
});

Make sure to remove listeners when they are no longer needed, it's better for performance:

// unbind all arrive events on document element
$(document).unbindArrive();

// unbind all arrive events on document element which are watching for ".test-elem" selector
$(document).unbindArrive(".test-elem");

// unbind only a specific callback
$(document).unbindArrive(callbackFunc);

// unbind only a specific callback on ".test-elem" selector
$(document).unbindArrive(".test-elem", callbackFunc);

// unbind all arrive events
Arrive.unbindAllArrive();

Options

As of v2.0 arrive event accepts an optional options object as 2nd argument. Options object consists of following:

var options = {
    fireOnAttributesModification: boolean, // Defaults to false. Setting it to true would make arrive event fire on existing elements which start to satisfy selector after some modification in DOM attributes (an arrive event won't fire twice for a single element even if the option is true). If false, it'd only fire for newly created elements.
    onceOnly: boolean                      // Defaults to false. Setting it to true would ensure that registered callbacks fire only once. No need to unbind the event if the attribute is set to true, it'll automatically unbind after firing once.
    existing: boolean                      // Defaults to false. Setting it to true would ensure that the registered callback is fired for the elements that already exist in the DOM and match the selector. If options.onceOnly is set, the callback is only called once with the first element matching the selector.
};

Example:

$(document).arrive(".test-elem", {fireOnAttributesModification: true}, function() {
    // 'this' refers to the newly created element
    var $newElem = $(this);
});

Watch for elements removal

Use leave event to watch for elements removal. The first arugument to leave must not be a descendent or child selector i.e. you cannot pass .page .test-elem, instead, pass .test-elem. It's because of a limitation in MutationObserver's api.

// watch for removal of an element which satisfies the selector ".test-elem"
$(".container-1").leave(".test-elem", function() {
    var $removedElem = $(this);
});

You can unbind the leave event in the same way as arrive event, using unbindLeave function i.e:

// unbind all leave events on document element
$(document).unbindLeave();

// unbind all leave events
Arrive.unbindAllLeave();

Browser Support

arrive.js is built over Mutation Observers which is introduced in DOM4. It's supported in latest versions of all popular browsers.

Browser Supported Versions
Google Chrome 27.0+
Firefox 14.0+
Safari 6.1+
Internet Explorer 11.0+
Opera 14.0+

Contributing

Report a bug / Request a feature

If you want to report a bug or request a feature, use the Issues section. Before creating a new issue, search the existing ones to make sure that you're not creating a duplicate. When reporting a bug, be sure to include OS/browser version and steps/code to reproduce the bug, a JSFiddle would be great.

Development

If you want to contribute to arrive, here is the workflow you should use:

  1. Fork the repository.
  2. Clone the forked repository locally.
  3. From the dev branch, create and checkout a new feature branch to work upon. (If you want to work on some minor bug fix, you can skip this step and continue to work in dev branch)
  4. Make your changes in that branch (the actual source file is /src/arrive.js).
  5. If sensible, add some jasmine tests in /tests/spec/arriveSpec.js file.
  6. Make sure there are no regressions by executing the unit tests by opening the file /tests/SpecRunner.html in a browser. There is a button 'Run tests without jQuery' at the top left of th page, click that button to make sure that the tests passes without jQuery. Run the test cases in all major browsers.
  7. Push the changes to your github repository.
  8. Submit a pull request from your repo back to the original repository.
  9. Once it is accepted, remember to pull those changes back into your develop branch!

Some features/bugs you can send pull requests for

  • #70: Add Typescript types to the project
  • #69: Option to watch for text change
  • #64: Function firing twice in Firefox 56 on 2.4.1 (regression from bug 54)
  • #60: Issue when expose arrive API to window

Keywords

javascript, js, jquery, node.js, watch, listen, creation, dynamically, removal, new, elements, DOM, dynamic, detect, insertions, event, bind, live, livequery

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].