Photo of Clay Gibson


Run JavaScript

Hey, does anyone know if there is a better way to run JavaScript on a block? Currently I am just running it inside of a script tag on an HTML block, but I would like to link to a file.  Is this possible?

  • Photo of Luke Potter


    Yes. It's possible to link a file just like you would in the head of a page. You'd just have to make sure the file is accessible from your current domain/subdomain.

    <script src="myscripts.js"></script>

    If you asking where to put it in Rock, I'd put it in the File Manager (Admin Tools > CMS Configuration > File Manager) or if you've configured an Storage site in your Asset Manager (AWS, Google Cloud Storage, Etc.) you could put it in the Asset Manager (Admin Tools > CMS Configuration > File Manager). Then just get the URL to the file & put it in the "src" attribute of the Script tag. 

    Hopefully this helps!

  • Photo of Shawn Ross


    Clay, FYI, I noticed you also had a duplicate question, so moving my info here. Luke is correct. If it helps at all, here's my input...

    I haven't done so with Javascript (I"m a newb there), but I believe you can use an Include tag to reference javascript.

    This would look like:

    As the HTML/Lava in your block

    {% include '~~/Assets/Lava/ClayTest.lava' %}

    As the contents of 'ClayTest.lava'

    document.getElementById("demo").innerHTML = "My First JavaScript";
    </script> <!-- Put whatever else you want here, such as a simple HTML statement to verify it works outside of the javascript -->

    How to upload 'ClayTest.lava'

    1. In Rock go to Admin Tools > CMS Configuration > File Manager
    2. Locate your theme directory (the double tilde [~~] indicates your theme directory)
    3. Navigate to the /Assets/Lava directory in your theme's directory
    4. Upload ClayTest.lava to the Lava directory