Reputation: 46979
How to embed the following inside a js file
<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
Upvotes: 4
Views: 8945
Reputation: 11
If you want to do this with pure JavaScript:
function loadJS(url) { //url is the string of the URL of the resource to fetch
var oReq = new XMLHttpRequest(); //create a new XMLHttpRequest and call it oReq
oReq.addEventListener("load", function(){eval(this.responseText)}); //evaluates the contents of the file once fetched
oReq.open("GET", url); //set the URL to fetch to the specified URL
oReq.overrideMimeType("text/plain; charset=x-user-defined"); //force the content of the file to be plain text
oReq.send(); //send the request
}
If you are doing anything with JavaScript and HTML, you should probably use Olical's solution (the one with the loadJS()
function). It is very useful. But, if for some reason you need to load an external JS file with only JS, this will work.
Basically, it creates and sends an XMLHttpRequest to get the specified URL's content in plain text. Once it gets that text, it evaluates it using eval()
. For those of you who do not know, eval()
is a function present in many programming languages (though sometimes named something else), including JavaScript. It takes the input of a string, and then treats the content of that string as if it were JavaScript code and runs it (called "evaluating" the statement, hence the name "eval"). For example, eval("console.log('test')")
would log 'test'
to the console, just like console.log("test")
would do. Clearly using it like I did in that example would make no sense (you can just state the code instead of putting in a string and passing that to eval()
!), but it is incredibly useful when you need to have your program create code to then run. For example, in you want to create an interactive JavaScript console with HTML and JavaScript, you could use eval()
to evaluate the user's input.
If you don't care about how it works and just want to use it, just add that function to your project and call that function with the parameter of the URL (in a string) that you want to include (for example, loadJS("www.foo.com/bar.js")
). I release this code snippet into the public domain, so if you want to use it, modify it, and or distribute it without credit, feel free to do so. I encourage (but not legally force you to) publish the project that you use this for with similar terms (Creative Commons, GPL, etc.). Hope that was helpful.
Upvotes: 1
Reputation: 15552
You may add it like this:
your_script.js:
document.write('<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>');
// ...
Then include only your_script.js
inside the HTML document.
EDIT: This is the dirty old method, you may use library or embed function.
Upvotes: -1
Reputation: 2385
You can attach it using JavaScript though.
jQuery has a nice method called getScript: http://api.jquery.com/jQuery.getScript/
Upvotes: 1
Reputation: 41452
You can not embed a JavaScript file in another. But you can load more JavaScript files in dynamically with a function such as this one.
function loadJS(file) {
// Grab the head element
var head = document.getElementsByTagName('head')[0];
// Create a script element
var script = document.createElement('script');
// Set the type
script.type = 'text/javascript';
// Set the source file
script.src = file;
// Add the script element to the head
head.appendChild(script);
}
Upvotes: 10
Reputation: 25049
You can't, you must embed both Javascript files into a HTML document.
Upvotes: 0