0

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 ;

5
Contributors
7
Replies
12
Views
9 Years
Discussion Span
Last Post by goldentuna
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
}
0

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

<link rel="stylesheet" type="text/css" href="all.css" />

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

Thanks for your above post

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...

0

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.

0

Can you have a js <script> tag inside another js <script> tag. We're trying to use ajax to load a page content into a div tag, and that content sometimes has javascript in it. ... other post we've made here with the question: Click Here

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.