How you can include a JavaScript file to another JavaScript

Many programmers have queries that how they can include a JavaScript file to another JavaScript, Is there any way to achieve ? The answers is JavaScript does not support methods include, import, required like other programming languages supports. But there are some other ways available in JavaScript to import/include JavaScript file in another JavaScript file.

1. Loading JavaScript file using Ajax.

It is an easy way to add JavaScript file in another JavaScript file using Ajax call and then call eval method. eval() function evaluate or execute JavaScript code or expression.  It is little risky if you are using eval() function in your code, it opens the door for bugs and hacks.

2. Loading JavaScript file using jQuery Function.

jQuery provides predefined function $.getScript to load JavaScript file.

$.getScript(“script-to-load.js”);

3. Another example of loading JavaScript file with in another JavaScript file.

<Javascript name=’coolexample’>
    <ResourceURL>jsc://coolexample.js</ResourceURL>
</Javascript>

So here i discuss several ways load JavaScript file, you can adopt any one of these method and if you have another solution of the problem then comment below, I should consider and it also help for the readers.

About Moazzam Qureshi

I am SEO expert and web developer since 2011. My hobby is to write & provides quality content about blogging, SEO , Social Media , Earn Online and Affiliate Marketing.