Git Product home page Git Product logo

ajax-fetch-data-loader's Introduction

Ajax Fetch() Data Loader

This small script allows you to insert HTML content and Javascript code retrieved in Ajax with the Javascript Fetch() API

Demo

https://www.ajax-fetch-data-loader.miglisoft.com

About

The Fetch() Javascript API is a simple and modern way to load dynamic content into a page. Unfortunately if the loaded content contains some Javascript code it will not be interpreted, that is why I provide this script.

Features

  • parse the content retrieved with Fetch
  • creates an Array with the HTML nodes and the scripts at the end
  • parses the Array and:
    • inserts the html nodes in the destination container
    • inserts the javascript into <script /> tags, which allows them to be executed.

Usage/Examples

<script>
    document.addEventListener('DOMContentLoaded', function(event) {
        fetch('data.html')
        .then(function (response) {
            return response.text()
        })
        .then(function (html) {
            console.info('content has been fetched from data.html');
            loadData(html, '#ajax-target').then(function (html) {
                console.info('I\'m a callback');
            });
        }).catch((error) => {
            console.log(error);
        });
    });
</script>

Alternatively you can append the fetched content to the HTML container instead of replacing it:

    // first fetch your content like in the first example above, then ...
    // the third "true" argument will append the fetched content into the container instead of replacing its original content.
    loadData(html, '#ajax-target', true).then(function (html) {
        console.info('I\'m a callback');
    });

Authors

ajax-fetch-data-loader's People

Contributors

migliori avatar

Watchers

 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.