Import external Javascript file code

Asked

Viewed 48 times

0

In CSS, there is the @import that I can call the code from an external file within my current file. Example:

@import 'font-awesome/css/all.min.css';

I wonder if there is something similar to this in Javascript? In case, make an "import" of all the contents of one Javascript file inside another.

1 answer

0

Hello, good afternoon!

One of the possible ways to do this is through modules, in this case the syntax is very simple, let’s assume that you want to export a function:

Function.js

export function helloWorld(){
    console.log('Hello world')
}

main.js

import { helloWorld } from './function.js';
helloWorld();

My explanation was very brief, but you can check a full documentation on the Mozila Developer website: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/M%C3%B3dulos

I hope I helped, good luck!

  • 1

    I’m not the one who denied it, but I think whoever did it was motivated by that phrase "import" of all the contents of one Javascript file within another. which implies something similar to var script = document.createElement('script'); script.src = CAMINHO_DO_SCRIPT; or something like the jQuery.getScript()

  • @Augustovasques would be something in the sense of what you said. Could you share as an answer for me score?

  • Do you want my comment as an answer? No! It’s just a comment guiding the AR which way to direct the answer. Has no complete or remote purpose in providing solution to the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.