Support » Plugin: Autoptimize » add js on scroll from top

  • Resolved p47ri0t

    (@p47ri0t)


    Hi, i use your perfect plugin

    js is not important for my theme in first seconds.

    i load other javascript(external js) by scrolling(just 1px from top).
    this made my page speed great.
    but how can i do it for js that AO create.

    I’m sorry my English is poor

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Optimizing Matters

    (@optimizingmatters)

    you would have to hook into Autoptimize’s API to change the way the JS is loaded p47ri0t.

    so how have you implemented the loading-by-scrolling?

    p47ri0t

    (@p47ri0t)

    @optimizingmatters i use this javascript code for static external js :

     function loadJS(file) {
        var jsElm = document.createElement("script");
        jsElm.type = "application/javascript";
        jsElm.src = file;
        document.body.appendChild(jsElm);
    }
    var addsanjagha = false;
    function addsanjagh()
    {
      if (addsanjagh === true) return;
       loadJS('/client.js');
       loadJS('/article.v1.js');
       loadJS('/yn_bnr.min.js');
       addsanjagha = true;
    }
    function initSanjagh()
    {
      if (addsanjagha === true) return;
    
      if($(window).scrollTop()>1){
        addsanjagh();
      }
    }
    
    $(document).scroll(function() {
      initSanjagh();
    });
    • This reply was modified 7 months ago by p47ri0t.
    Plugin Author Optimizing Matters

    (@optimizingmatters)

    hmm, not do-able in a couple of lines of code, but -assuming you’re OK writing some PHP- best would be to hook into the autoptimize_html_after_minify filter and using that to extract the last script tag (before </body>) which holds the link to the autopitimized JS, remove it from the HTML and add the JS instead?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add js on scroll from top’ is closed to new replies.