How do I include a JavaScript file in another JavaScript file

0 votes
Is there something in JavaScript similar to @import in CSS that allows you to include a JavaScript file inside another JavaScript file?
Jul 27, 2020 in Java-Script by kartik
• 37,520 points
833 views

1 answer to this question.

0 votes

Hello @kartik,

It is possible to dynamically generate a JavaScript tag and append it to HTML document from inside other JavaScript code. This will load targeted JavaScript file.

function includeJs(jsFilePath) {
    var js = document.createElement("script");

    js.type = "text/javascript";
    js.src = jsFilePath;

    document.body.appendChild(js);
}

includeJs("/path/to/some/file.js");

Hope it helps!!

Thank you!!

answered Jul 27, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

How do I break a string across more than one line of code in JavaScript?

Hello @kartik, In your example, you can break ...READ MORE

answered Oct 8, 2020 in Java-Script by Niroj
• 82,840 points
665 views
0 votes
1 answer

How do I link a JavaScript file to a HTML file?

Hello @kartik, This is how you link a ...READ MORE

answered Oct 8, 2020 in Java-Script by Niroj
• 82,840 points
664 views
0 votes
1 answer

How do I search for a key of object in javascript?

Use hasOwnProperty(key) for (let i = 0; i ...READ MORE

answered Oct 14, 2020 in Java-Script by Niroj
• 82,840 points
2,496 views
0 votes
0 answers

How do I format a date in JavaScript?

May 7, 2022 in Java-Script by narikkadan
• 63,600 points
464 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,994 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,691 views
0 votes
1 answer

How do I add a delay in a JavaScript loop?

Hello @kartik, The setTimeout() function is non-blocking and will return ...READ MORE

answered Sep 21, 2020 in Java-Script by Niroj
• 82,840 points
7,763 views
0 votes
1 answer

How do I declare a namespace in JavaScript?

Hello @kartik, Try this: var yourNamespace = { ...READ MORE

answered Sep 21, 2020 in Java-Script by Niroj
• 82,840 points
581 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP