2

I would like to know how to import a .js file inside another .js file just like how we import css file inside another css file

all.css file goes like this.....

 @import "simple1.css";
 @import "simple2.css";
 @import "simple3.css";
 @import "simple4.css";

same way i wanna import js/jquery-1.7.1.min.js in my custom.js file

custom.js file goes like this

 $(document).ready(function(){
 $("#AddImage").html("<img src='"+clientID+".jpg'/>");
 });

i don't want to include js/jquery-1.7.1.min.js like

 <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>

i want to include it in my custom.js file.

Please help! Thanks in advance!!!

asked Jul 12, 2012 at 11:35
2
  • stackoverflow.com/questions/950087/… Commented Jul 12, 2012 at 11:37
  • no it didnt work for me.... is there some simple code to import it Commented Jul 12, 2012 at 11:39

3 Answers 3

4

You can create function like below.

function importJs(url)
{
 var head = document.getElementsByTagName('head')[0];
 var script = document.createElement('script');
 script.type = 'text/javascript';
 script.src = url;
 head.appendChild(script);
}

to run the code, see example below

importJs("myScript.js");

Now you can use importJs() in your any js. Which you can use for JS , similar to @import in CSS.

answered Oct 22, 2014 at 8:13
Sign up to request clarification or add additional context in comments.

Comments

2

this code worked for me using jQuery

$.getScript("myscript.js");
answered Jul 12, 2012 at 11:49

Comments

1

Ther is this plugin http://code.google.com/p/jquery-include/. This jQuery plugin provides browser based file include similar to SSI.

$(document).includeReady(function () {
 // initialisation code here
 // e.g. modify include dom
});

And to use it

<span data-src="includes/test1.html">
 <p class="error-msg">include has not loaded</p>
</span>
answered Jul 12, 2012 at 11:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.