Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon 692 posts 1068 karma points
    Mar 12, 2016 @ 14:28
    Simon
    0

    Optimus Bundling Package - Load after page Load

    Hi Guys,

    I am usiing the Optimus Package for bundling and minification.

    To increase website performace I want to load bundles after page load to increase performace, as stated here :https://varvy.com/pagespeed/defer-loading-javascript.html

    Now, I was going to use this function, without using even the jquery

    <script type="text/javascript">
    function downloadJSAtOnload() {
    var element = document.createElement("script");
    element.src = "defer.js";
    document.body.appendChild(element);
    }
    if (window.addEventListener)
    window.addEventListener("load", downloadJSAtOnload, false);
    else if (window.attachEvent)
    window.attachEvent("onload", downloadJSAtOnload);
    else window.onload = downloadJSAtOnload;
    </script>
    

    But I want the link to bundle and I don't know how to get only the link.

    Any ideas please or else how to tackle this, in order load JavaScript, through bundling, after page loads?

    Thank you

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 12, 2016 @ 15:35
    Dave Woestenborghs
    0

    Hi Simon,

    I guess you have this code in your template somewhere.

    So you can modify it like this :

    <script type="text/javascript">
    function downloadJSAtOnload() {
    var element = document.createElement("script");
    element.src = "@Scripts.Render("~/pathtoscriptbundlehere")";
    document.body.appendChild(element);
    }
    if (window.addEventListener)
    window.addEventListener("load", downloadJSAtOnload, false);
    else if (window.attachEvent)
    window.attachEvent("onload", downloadJSAtOnload);
    else window.onload = downloadJSAtOnload;
    </script>
    

    Dave

  • Simon 692 posts 1068 karma points
    Mar 12, 2016 @ 16:14
    Simon
    0

    Currently, I am using

    @Scripts.Render("~/bundles/global")

  • Simon 692 posts 1068 karma points
    Mar 12, 2016 @ 16:20
    Simon
    0

    Hi Dave,

    I have tried it in that way but it does not works.

    @Scripts.Render will not render the script src only but including the scripts tags etc.

    All I want is the actual link to src, which results eventaully to /bundles/hj4h234hh2h4h2424h2h4

    Thanks

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Mar 12, 2016 @ 16:37
    Dave Woestenborghs
    0

    hi Simon,

    I believe you can do :

    @Scripts.Url("~/bundles/global")
    

    This should only output the URL

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft