# JavaScript, Handlebars, and OnLoad functions

**URL:** https://forum.kirupa.com/t/javascript-handlebars-and-onload-functions/641247
**Category:** programming
**Created:** [December 4, 2019, 5:37pm UTC](https://forum.kirupa.com/t/javascript-handlebars-and-onload-functions/641247 "2019-12-04T17:37:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dannywolf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dannywolf/32/10471_2.png) [@dannywolf](https://forum.kirupa.com/u/dannywolf)
#### Post date: [December 4, 2019, 5:37pm UTC](https://forum.kirupa.com/t/javascript-handlebars-and-onload-functions/641247/1 "2019-12-04T17:37:24Z")

</div>

I am using Handlebars in a Node/Express project. It works beautifully and its layouts, templates, and partials are a blessing. I have one question, though:

Where would the best place to place to put my JavaScript files import be?

This is the default layout I am currently using…

```
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="css/styles.css">
  <title>{{title}}</title>
</head>
<body>

  {{{ body }}}

  <script src="https://cdn.net/blahblahblah/jquery.min.js"></script>
  <script src="https://cdn.net/blahblahblah/bootstrap.min.js"></script>
  <script src="js/scripts.js" charset="utf-8"></script>
</body>
</html>

```

For those who may not know, Handlebars will insert my _templates_ where the _{{{ body }}}_ instruction is, which means I can focus on separate templates for different pages without bothering to repeat the same common structure(s).

As is, I get a _NotDefined_ error when I try to run a JS function from the **template** when it loads. That makes sense since the **template** loads before the **layout** has a chance to load my scripts.js (last one to be loaded).

I can place the script loading statements _inside_ the template, but that defeats the purpose of using templates as I would be duplicating code.

And, if I place the script loading statements _early_ in the layout file, say within the **head** tag or immediately after opening the **body** tag, I fear I will have problems with performance.

So, where do you guys put your script loading statements in this situation?

---

<div class="post-metadata">

### Author: ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)
#### Post date: [December 4, 2019, 9:43pm UTC](https://forum.kirupa.com/t/javascript-handlebars-and-onload-functions/641247/2 "2019-12-04T21:43:09Z")

</div>

If you have scripts in {{{body}}} that depend on the scripts lower in the page, then you either have to defer the running of the injected scripts (e.g. in document onload or equiv) or include your other scripts before {{{body}}}

---

<div class="post-metadata">

### Author: ![dannywolf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/dannywolf/32/10471_2.png) [@dannywolf](https://forum.kirupa.com/u/dannywolf)
#### Post date: [December 5, 2019, 7:43pm UTC](https://forum.kirupa.com/t/javascript-handlebars-and-onload-functions/641247/3 "2019-12-05T19:43:51Z")

</div>

Yes… I was wondering if there was a different way to tackle the problem using Handlebars. ☹ Thanks for taking the time to reply, though. 🙂

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [December 6, 2019, 5:45am UTC](https://forum.kirupa.com/t/javascript-handlebars-and-onload-functions/641247/4 "2019-12-06T05:45:39Z")

</div>

Unfortunately, no. This is a minor hassle of scripts that deal with layout and content. You have to run them ahead of everything else that your page might need for displaying itself. There will be a performance impact, but it won’t be too bad since handlebars is fairly small and optimized. You should ensure any non-templating code runs after page load.
