Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript using "import from" dynamically

hello -

i have used the following syntax a number of times with much success:
JavaScript:
        let script    = document.createElement('script')              ;
        script.src      = '/js/myJavaScript.js'                    ;
        script.type     = 'text/javascript'                         ;
        script.async    = false                                         ;
        const scripts   = document.getElementsByTagName('script')[0]    ;
        scripts.parentNode.insertBefore(script, scripts)                ;
however, i am unable to determine how to use this same approach to replace the following code:
JavaScript:
import myName from '/js/myJavaScript.js' ;
is this even possible?
 
hello -

i have used the following syntax a number of times with much success:
JavaScript:
        let script    = document.createElement('script')              ;
        script.src      = '/js/myJavaScript.js'                    ;
        script.type     = 'text/javascript'                         ;
        script.async    = false                                         ;
        const scripts   = document.getElementsByTagName('script')[0]    ;
        scripts.parentNode.insertBefore(script, scripts)                ;
however, i am unable to determine how to use this same approach to replace the following code:
JavaScript:
import myName from '/js/myJavaScript.js' ;
is this even possible?
Edit - adding this:
Code:
script.innerText =  "import myName from '/js/myJavaScript'";
gets me an identical value in: document.getElementsByTagName('script')[0] as the import myName from does, but sadly still does not work.
Code:
console.log(document.getElementsByTagName('script')[0]  )       ;
// result:  <script type="module">import myName from '/js/myJavaScript'</script>
 
Last edited:
i figured it out:

Code:
var WaveSurfer = null;
let script        = document.createElement('script')        ;
script.type        = 'module'                ;
script.innerText    = "import ws from 'https://unpkg.com/wavesurfer.js@beta'; WaveSurfer=ws;";
document.head.appendChild(script);
the solution is to use a global variable - unless one of you wizards have a better solution...?
 
Back
Top Bottom