2

ok let's say I have these two links

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>

I'm using these as my javascript sources

and I created another file called

<script src="testing.js"></script>

but in my html I want to show the script src="testing.js" I am able to somehow put the first two links into the testing.js file?

so in my html it'll only show

<script src="testing.js"></script>

but actually it's running three scripts...

NetStarter
  • 3,189
  • 7
  • 37
  • 48
Dora
  • 6,776
  • 14
  • 51
  • 99

4 Answers4

2

You cannot directly link a .js file from a .js file

Check this: How do I include a JavaScript file in another JavaScript file?

It clearly says what can you do to link a .js in a .js file

Community
  • 1
  • 1
Hari krishnan
  • 2,060
  • 4
  • 18
  • 27
1

Solution from about.com:

In your testing.js file create this function:

function addJavascript(jsname,pos) {
var th = document.getElementsByTagName(pos)[0];
var s = document.createElement('script');
s.setAttribute('type','text/javascript');
s.setAttribute('src',jsname);
th.appendChild(s);
}

Later call the function using:

addJavascript('http://code.jquery.com/jquery-latest.js','head');
addJavascript('http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js','head');

There are few other ways, but not all of them are compatible with all browsers.

Tomasz Łoś
  • 79
  • 2
  • 10
0

You can only include a script file in an HTML page, not in another script file

vborutenko
  • 4,323
  • 5
  • 28
  • 48
0

You can use requireJS for Asynchronous loading of JS files. If that is an option, A tutorial to get you started can be found here.

P.S. I know that this is not what OP asked but the requirement is very similar to this.

gashu
  • 484
  • 6
  • 17