lundi 5 décembre 2016

Dynamically load jquery in plain javascript

I am taking a page that does not have jquery referenced in the dom. I need to use some of the jquery functionality, so i figured the best bet is to inject jquery into the existing dom. I got the function idea from Load jQuery with Javascript and use jQuery and How to include jQuery dynamically in any website using pure javascript ....none of these solutions seem to work for me. I am still getting an error not recognizing the jquery selector.

(function() {
    function getScript(url, success) {
        var script = document.createElement('script');
        script.src = url;
        var head = document.getElementsByTagName('head')[0],
            done = false;
        // Attach handlers for all browsers
        script.onload = script.onreadystatechange = function() {
          if (!done && (!this.readyState
               || this.readyState == 'loaded'
               || this.readyState == 'complete')) {
            done = true;
            success();
            script.onload = script.onreadystatechange = null;
            head.removeChild(script);
          }
        };
        head.appendChild(script);
    }
    getScript('http://ift.tt/2a1eCsp',function() {
        // Yay jQuery is ready \o/
    });                         


    cards = $('div:first');
    $('body').empty().append(cards);

    // Delete first and second child divs

    first = $('div:first div:first');
    $('div:first div:first').css('position', '').css('left', '').css('z-index', '').css('height', '250px').remove();

    second = $('div:first div:first');
    $('div:first div:first').css('position', 'relative').css('left', '').css('z-index', '').remove();

    $('body').empty().append(first).append(second);
    })();




Aucun commentaire:

Enregistrer un commentaire