User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 429,971 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,572 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 6342 | Replies: 6
Reply
Join Date: Sep 2006
Posts: 26
Reputation: Alexandro is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Alexandro Alexandro is offline Offline
Light Poster

Import javascript in javascript

  #1  
May 20th, 2007
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 ;
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,871
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 345
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Import javascript in javascript

  #2  
May 20th, 2007
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.

  1. if (typeof(km_scripts) == 'undefined') var km_scripts = new Object();
  2. km_myclass_import('importedfile.js');
  3.  
  4. function km_myclass_import(jsFile) {
  5. if (km_scripts[jsFile] != null) return;
  6. var scriptElt = document.createElement('script');
  7. scriptElt.type = 'text/javascript';
  8. scriptElt.src = jsFile;
  9. document.getElementsByTagName('head')[0].appendChild(scriptElt);
  10. km_scripts[jsFile] = jsFile; // or whatever value your prefer
  11. }
Last edited by ~s.o.s~ : May 20th, 2007 at 9:38 pm.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Sep 2006
Posts: 26
Reputation: Alexandro is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
Alexandro Alexandro is offline Offline
Light Poster

Re: Import javascript in javascript

  #3  
May 21st, 2007
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
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Posting Shark

Re: Import javascript in javascript

  #4  
May 21st, 2007
If you have access to PHP:
  1. <?php
  2. header("Content-Type: text/javascript");
  3. include "script1.js";
  4. include "script2.js";
  5. ?>
Save as 'allscripts.php' and link as if it were a js file:
  1. <script type="text/javascript" src="allscripts.php"><!--screwIE6--></script>
This might not be ideal in all situations...
Last edited by MattEvans : May 21st, 2007 at 8:37 am. Reason: forgot to screw IE6..
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,871
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 345
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Import javascript in javascript

  #5  
May 21st, 2007
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.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Jan 2007
Posts: 2,565
Reputation: MidiMagic is on a distinguished road 
Rep Power: 7
Solved Threads: 115
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Maven

Re: Import javascript in javascript

  #6  
May 21st, 2007
You can have more than one script tag in your hrml.
Daylight-saving time uses more gasoline
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,871
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 345
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Import javascript in javascript

  #7  
May 21st, 2007
But I guess that is not what he is looking for.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 11:56 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC