Solved

include javascript file to javascript file....

Posted on 2008-10-26
6
270 Views
Last Modified: 2008-10-27
Hello Experts,
I have a web project with a big number of pages. Each page has numerous javascript files included (libraries and my own) this way:
<script type="text/javascript" src="somefile.js"></script>

I don't like to have this kind of mess... is it possible to include only one js file, and inside that file to have all the libraries included?
If yes, what is the syntax?
0
Comment
Question by:vvvlad42
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 16

Assisted Solution

by:Gyanendra Singh
Gyanendra Singh earned 150 total points
ID: 22810475
At the beginning of each .js file importing another file:
(this file defines a class called 'myclass' and 'km_' is just the prefix I
use for my functions or classes)

----------------------------------------------------------------
// myclass.js

if (typeof(km_scripts) == 'undefined') var km_scripts = new Object();
km_myclass_import('importedfile.js');

function km_myclass_import(jsFile) {
if (km_scripts[jsFile] != null) return;
var scriptElt = document.createElement('script');
scriptElt.type = 'text/javascript';
scriptElt.src = jsFile;
document.getElementsByTagName('head')[0].appendChild(scriptElt);
km_scripts[jsFile] = jsFile; // or whatever value your prefer
}

function km_myclass_alert() {
alert(importedValue);
}

-------------------------------------------------------------
The imported file importedfile.js:

// importedfile.js

var importedValue = 'The file was imported';

----------------------------------------------------------------
The HTML file to test it :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<script language="javascript" src="myclass.js" />
<TITLE></TITLE>
</HEAD>
<BODY>
<input type="button" value="Test!" onclick="km_myclass_alert()" />
</BODY>
</HTML>
0
 
LVL 16

Expert Comment

by:sh0e
ID: 22810478

function loadJS(src) {  
var script = document.createElement('script');  
script.src = src;  
script.type = 'text/javascript';  
var head = document.getElementsByTagName('head')[0];  
if (!head) {  
head = document.body.parentNode.appendChild(document.createElement('head'));  
}  
head.appendChild(script);  
}
 
loadJS('somefile.js');

Open in new window

0
 

Author Comment

by:vvvlad42
ID: 22810506
So I'll have to do all that for each file? I have at least 6....
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:sh0e
ID: 22810570
You just have to call loadJS('somefile.js')
The function only has to be declared once.

You can just put everything into one JS file.
BondinASP has exactly the same code, except he added a lot of wrapping code.

If you would like to declare an array of files to include, put this into a .js file and include it:
var myscripts = [
"somefile.js","another.js","twofold.js"
];
 
//No need to modify code below
function loadJS(src) {  
var script = document.createElement('script');  
script.src = src;  
script.type = 'text/javascript';  
var head = document.getElementsByTagName('head')[0];  
if (!head) {  
head = document.body.parentNode.appendChild(document.createElement('head'));  
}  
head.appendChild(script);  
}
 
for(int i=0;i<myscripts.length;i++) loadJS(myscripts[i]);

Open in new window

0
 
LVL 16

Accepted Solution

by:
sh0e earned 350 total points
ID: 22810574
Sorry, made a typo above.  Used int instead of var.
var myscripts = [
"somefile.js","another.js","twofold.js"
];
 
//No need to modify code below
function loadJS(src) {  
var script = document.createElement('script');  
script.src = src;  
script.type = 'text/javascript';  
var head = document.getElementsByTagName('head')[0];  
if (!head) {  
head = document.body.parentNode.appendChild(document.createElement('head'));  
}  
head.appendChild(script);  
}
 
for(var i=0;i<myscripts.length;i++) loadJS(myscripts[i]);

Open in new window

0
 

Author Comment

by:vvvlad42
ID: 22810663
Hi,
Just a small clarification...
What if my js file is located in a subfolder? how do I provide a path?

Thank you again
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript object functions help 1 19
Angular tyeahead feature not working with Control "as" ControlXXX 4 19
Two decimal 5 30
Diff of the day 2 11
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question