CMS Development

xyboox
Participant

How to reference a javascript from within another

SOLVE

I am trying to implement some javascript code into our cms webpage template. So I have a javascript file which I use with the require_js function:

{{ require_js(get_asset_url("../js/main.bundle.js")) }}


Somewhere in this main.bundle.js I have code that uses a worker from another file. In standalone code, this is:

var w = new Worker("./worker.js")

 

As is, the worker.js file is not imported when used in HS cms. In console, I get a 404 for the file. I tried:

var w = new Worker("/hs-fs/${hs_id}/theme_name/js/worker.js")
var w = new Worker("https://our-domain.com/hs-fs/${hs_id}/theme_name/js/worker.js")

none the above works. Always 404. 

Any advise please?
Thanks!
 

0 Upvotes
1 Accepted solution
alyssamwilie
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

How to reference a javascript from within another

SOLVE

Design manager file URLs update each time you publish them so there's no set path you can use. You'd either need to:

 

  • Add your file to the page so you can use it's public URL
  • OR upload your file to the file manager and use that URL
  • OR set a global variable in a <script> in your HTML template so that you can use the get_asset_url() HubL function (because HubL isn't usuable inside javascript files).

 

 

<script>
  window.worker = "{{ get_asset_url('relative/file/path/here/file.js') }}";
</script>

 

 

 

If this answer solved your question, please mark it as the solution.

Alyssa Wilie Profile Image

Alyssa Wilie

Web Developerat Lynton

Learn HubL | Get Marketing Insights

HubSpot Elite Solutions Partner
Lynton's HubSpot theme Rubric now available. Click to download.

View solution in original post

2 Replies 2
alyssamwilie
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

How to reference a javascript from within another

SOLVE

Design manager file URLs update each time you publish them so there's no set path you can use. You'd either need to:

 

  • Add your file to the page so you can use it's public URL
  • OR upload your file to the file manager and use that URL
  • OR set a global variable in a <script> in your HTML template so that you can use the get_asset_url() HubL function (because HubL isn't usuable inside javascript files).

 

 

<script>
  window.worker = "{{ get_asset_url('relative/file/path/here/file.js') }}";
</script>

 

 

 

If this answer solved your question, please mark it as the solution.

Alyssa Wilie Profile Image

Alyssa Wilie

Web Developerat Lynton

Learn HubL | Get Marketing Insights

HubSpot Elite Solutions Partner
Lynton's HubSpot theme Rubric now available. Click to download.
Jaycee_Lewis
Community Manager
Community Manager

How to reference a javascript from within another

SOLVE

Hi, @xyboox 👋 Thank you for the question. Hey, @Indra @miljkovicmisa, do you have any JavaScript wisdom you can share? Or thoughts on how @xyboox might approach this challenge?

 

Thank you for taking a look! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes