# Add javascript to a theme component

**URL:** https://meta.discourse.org/t/add-javascript-to-a-theme-component/152480
**Category:** Development
**Created:** [May 23, 2020, 7:36pm UTC](https://meta.discourse.org/t/add-javascript-to-a-theme-component/152480 "2020-05-23T19:36:57Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![sbernhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sbernhard/32/208186_2.png) [@sbernhard](https://meta.discourse.org/u/sbernhard)
#### Post date: [May 23, 2020, 7:36pm UTC](https://meta.discourse.org/t/add-javascript-to-a-theme-component/152480/1 "2020-05-23T19:36:58Z")

</div>

Is there a good way to add javascript (one file, minimized) to a theme component so that it can be used in common/header.html?

---

<div class="post-metadata">

### Author: ![sbernhard](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sbernhard/32/208186_2.png) [@sbernhard](https://meta.discourse.org/u/sbernhard)
#### Post date: [May 24, 2020, 7:54am UTC](https://meta.discourse.org/t/add-javascript-to-a-theme-component/152480/2 "2020-05-24T07:54:45Z")

</div>

Got it working. Just added the JS (not minimized) into the common/header.html.

See [discourse-highlight-text/common/header.html at master · sbernhard/discourse-highlight-text · GitHub](https://github.com/sbernhard/discourse-highlight-text/blob/master/common/header.html)

Would be good if someone could document this behavior to the theme /component developer guide.

---

<div class="post-metadata">

### Author: ![Amethi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amethi/32/88690_2.png) [@Amethi](https://meta.discourse.org/u/Amethi)
#### Post date: [June 17, 2020, 12:10pm UTC](https://meta.discourse.org/t/add-javascript-to-a-theme-component/152480/3 "2020-06-17T12:10:41Z")

</div>

How did you use the JS in your component? I’m using the Versatile Banner component and want to add a button that calls my custom JS function. I’ve put in the head as you mention but get a CSP error:

`dev.londonbikers.com/:1 Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src https://dev.londonbikers.com/logs/ https://dev.londonbikers.com/sidekiq/ https://dev.londonbikers.com/mini-profiler-resources/ https://dev.londonbikers.com/assets/ https://dev.londonbikers.com/brotli_asset/ https://dev.londonbikers.com/extra-locales/ https://dev.londonbikers.com/highlight-js/ https://dev.londonbikers.com/javascripts/ https://dev.londonbikers.com/plugins/ https://dev.londonbikers.com/theme-javascripts/ https://dev.londonbikers.com/svg-sprite/". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.`

When calling my code via `onclick="NavigateToCustomLoginUrl('facebook')"` on a button.

---

<div class="post-metadata">

### Author: ![Amethi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amethi/32/88690_2.png) [@Amethi](https://meta.discourse.org/u/Amethi)
#### Post date: [June 17, 2020, 12:19pm UTC](https://meta.discourse.org/t/add-javascript-to-a-theme-component/152480/4 "2020-06-17T12:19:39Z")

</div>

Oh, think I answered my own question via [Content security policy &nbsp;|&nbsp; Articles &nbsp;|&nbsp; web.dev](https://www.html5rocks.com/en/tutorials/security/content-security-policy/#inline-code-considered-harmful)

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [June 17, 2020, 4:08pm UTC](https://meta.discourse.org/t/add-javascript-to-a-theme-component/152480/5 "2020-06-17T16:08:18Z")

</div>

Check `loadScript` in the codebase/meta for a systematic way to achieve this.
