简体   繁体   中英

script src using relative path and a different port

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>

@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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM