Giter Site home page Giter Site logo

checkifnew.js's Introduction

#checkIfNew.js

This small function was built with the intention to watch for additions or subtractions to elements of a tag name in the DOM. It does not notify you if you change an element, or if you replace elements by removing them and adding an element. Since this uses requestAnimationFrame for a loop: there is a chance this will fire IF these events take longer than it takes for the browser to fire requestAnimationFrame (60fps). Although it does not work by switching DOM nodes that fall before the final node in the list, if you remove an element, and add one to the end, it will be considered changed. So if you take the first instance of a div and append it to the end of the document, checkIfNew will say the list has changed. eg.

checkIfNew is build upon the fact that getElementsByTagName returns a LIVE nodeList. This means that the nodeList will change if elements change on the page. This makes for less work when checking for changes in javascript. This means it will be faster executing the loop that checks for changes, because it does not have to do a query for new elements in every loop. getElementsByTagName returns a live nodeList as per the spec, unlike querySelectorAll, so this is not relying on something that will change in the future.

document.body.appendChild(document.querySelector("div"))

will report a change.

var firstDiv = document.querySelector("div");
var switcher = document.querySelector("div:nth-child(2)");
firstDiv.parentNode.insertBefore(document.querySelector(switcher, firstDiv);

will NOT report a change.

This is because checkIfNew only watches the length, and changing of the final element of a nodeList.

You use it by assigning a tag type (div, img, a, section etc.), and giving it a callback that returns the current array of DOM nodes available on the page every time one is added or removed.

var divs = checkIfNew({"tag": "div"}, function(newList) {

  console.log(newList);

});
divs.loopList();

to assign a parent element to use as a scope for your tags you add a "parent" to the first argument's object. For example:

var container = document.getElementById("container");
var sectionInContainer = checkIfNew({"tag": "section", "parent": container}, function(change) {

    console.log(change);

  });

will look for changes to all <section>s within the parent container with the ID #container

checkifnew.js's People

Contributors

willsonsmith avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.