Javscript won't load when data is passed

So it took me a good bit to realize why my javascript references in the header would load sometimes and not others. I realized it was when I passed data to the page. When I pass data it seems that the script files don’t load properly and I get an error saying there is an extra > in my script. But it works fine when no data is passed.

What is causing this and how do I fix it?

If your scripts are in the header, maybe you are trying to use data that hasn’t been loaded yet, so they could fail.
Try to addDOMContentLoaded event to make sure your script runs only after the entire DOM has been fully loaded.