954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Import javascript in javascript

I know that I can import an css file in other css file with @import
Ex: @import "style.css"; and i use this in allcss.css

Can I do something like this in javascript, import a javascript in other javascript ;

Alexandro
Light Poster
26 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

Create a new element of type 'script' using document.createElement('script') and set its properties accordingly. Something I found on one of the sites. Write this at the top of the script file in which you plan on including your file.

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
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

I am looking for something more simple,like in css.
The css example:

Begin with four stylesheets: simple1.css ,simple2.css,simple3.css,simple4.css reffering to navigation,headers,
special styles...

I want to load them at once,so I make another css file all.css

all.css - code

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

the xhtml page code

so,I import all files in only one http request
I am looking for something similar in java script

Thanks for your above post

Alexandro
Light Poster
26 posts since Sep 2006
Reputation Points: 10
Solved Threads: 0
 

If you have access to PHP:

<?php 
header("Content-Type: text/javascript");
include "script1.js";
include "script2.js";
?>

Save as 'allscripts.php' and link as if it were a js file:

<script type="text/javascript" src="allscripts.php"><!--screwIE6--></script>

This might not be ideal in all situations...

MattEvans
Veteran Poster
Moderator
1,386 posts since Jul 2006
Reputation Points: 522
Solved Threads: 64
 

Alex, there is as such no 'simple' way of doing the thing you are trying to achieve. If yours is a client side only Javascript application, use the approach which I had mentioned in my previous post. If you are using your Javascript file in a dynamic page, use the approach provided by Matt.

Each and every server side language has its own way of doing it. If using PHP, use the method specified by Matt. If using JSP, you can use the include directive to do the same.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

You can have more than one script tag in your hrml.

MidiMagic
Nearly a Senior Poster
3,319 posts since Jan 2007
Reputation Points: 730
Solved Threads: 182
 

But I guess that is not what he is looking for.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You