1

There are all sorts of great ways to have an href with a relative path and different port on click, but I can't find anything to do the same for a <script>'s src attribute.

How can I approximate this?

<script src=":8000/same/host/different/port.js"></script>
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
  • 2
    Why not create the script tag on the go, from the example you’ve linked to, and address the port and path to the script in the same manner? E.g. create script element, set the src and append to body? – Cue Aug 04 '19 at 02:12
  • Does it have to be client side? – MinusFour Aug 04 '19 at 02:20
  • @MinusFour the server side context is a very small step above a static web page, so in this case I believe yes – Ryan Haining Aug 04 '19 at 03:34

1 Answers1

2

@Cue made me realize there is a solution in the linked question, using Craig McQueen's second suggestion, I can get the path.

const script = document.createElement('script');
const scriptSrc = (window.location.protocol + '//' + 
                   window.location.hostname + 
                   ':8000/same/host/different/port.js');
script.setAttribute('src', scriptSrc);
document.head.appendChild(script);
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
  • In this scenario, can we guarantee that the javascript in port.js will be executed before it continues parsing the rest of the page? I believe that if using a classic – Max Carroll Jan 26 '21 at 09:48
  • @MaxCarroll I'm not sure what exactly you mean by "before it continues parsing." Could you explain the problem that you are trying to avoid? – Ryan Haining Jan 26 '21 at 19:57