Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Import javascript in javascript
Old 05-20-2007, 03:36 AM Import javascript in javascript
Average Talker

Posts: 25
Trades: 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 ;
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Alexandro is offline
Reply With Quote
View Public Profile Visit Alexandro's homepage!
 
 
Register now for full access!
Old 05-21-2007, 10:03 AM Re: Import javascript in javascript
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
Try this:
Code:
<script type="text/javascript">
function importScript(url){
    var tag = document.createElement("script");
    tag.type="text/javascript";
    tag.src = url;
    document.body.appendChild(tag);
}
window.onload = function(){
    // imports go here
    importScript("foo.js"); // example
};
</script>
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 02-21-2009, 04:02 PM Re: Import javascript in javascript
Junior Talker

Posts: 3
Name: Andrei C
Location: Romania
Trades: 0
I am actually looking for a way to do this in JS, just like in php : include("filename");

I am doing this because i want to reduce http requests, bcause i have to load a few js scripts. is it good to do this?
andreickw is offline
Reply With Quote
View Public Profile Visit andreickw's homepage!
 
Old 02-21-2009, 04:16 PM Re: Import javascript in javascript
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
I am doing this because i want to reduce http requests
The only way to do that in js is by merging all the files in 1.
Javascript being a client side language, including it from another javascript will still make 1 request to your server.
The source has to be read.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 02-23-2009, 11:37 AM Re: Import javascript in javascript
Junior Talker

Posts: 3
Name: Andrei C
Location: Romania
Trades: 0
Ya, i got that,

Thanks tripy!
andreickw is offline
Reply With Quote
View Public Profile Visit andreickw's homepage!
 
Reply     « Reply to Import javascript in javascript
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14550 seconds with 12 queries