4

How to embed the following inside a js file

    <script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
1

5 Answers 5

10

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

This function is stolen from the load function of my library. github.com/Wolfy87/Spark/blob/master/src/load.js
not sure 'stolen' is appropriate when you posted it yourself!
there's not a lot of ways of doing that anyway ;)
I know, I was just saying where it was from ^^ and yeah, simple is better when it comes to JavaScript. Well, usually anyway. It doesn't look like Rajeev is going to accept anything at this rate either. If it is not working, tell us :]
1

You can attach it using JavaScript though.

jQuery has a nice method called getScript: http://api.jquery.com/jQuery.getScript/

Comments

1

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.

Comments

0

You can't, you must embed both Javascript files into a HTML document.

Comments

-1

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.

2 Comments

I don't like this answer for a lot of reasons (mainly the dirty old part), but this might also have problems depending on the speed of the connection - I'm assuming the author wants to use swfobject in his own JS files, and if he makes the call to swfobject before the browser has finished loading the secondary file, it will error.
Sadly I see this much anticipated method very often, provided by adserver firms. Yes, it is a bad practice, but I think it is better to be avare of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.