0

I need to put the entire following code in a javascript called activate.js. Can you tell me how?

<script type="text/javascript" src="http://domain.com/tyr1.js"></script>
<script type="text/javascript">
var int = new int({
    seconds: 30,
    markUser: true,
    markPeriod: 0
});
pop.add("http://www.google.com", "google", "0");
pop.add("http://www.yahoo.com", "yahoo", "1");
pop.add("http://www.bing.com", "bing", "2");
pop.show();
</script>
2
  • In short, without manually combining the code as @Andrew suggests, you can't do quite what you want since JavaScript itself has no mechanism for including other JavaScript files. That needs to be done in HTML with the script element. Having said that, your activate.js could potentially create the script elements in the DOM, requesting the appropriate files. Commented Mar 27, 2012 at 16:45
  • Thanks Josh, can you tell me how? Commented Mar 27, 2012 at 17:00

2 Answers 2

2

As mentioned in my comment above, you can't do quite what you want since JavaScript itself has no mechanism for including other JavaScript files. JavaScript files are included in the document using the HTML script element.

However, we could write one script ("active.js") that we include in the document and this manually creates an additional script element for each external JavaScript file. It should be noted, however, that this could suffer a degradation in performance since we are creating additional HTTP requests.

I think you will also need to separate your current inline script into an external file in order to avoid a "chicken vs egg" scenario where you are referring to elements of "tyr1.js" before it has loaded.

So your "active.js" script becomes something like:

/**
 * Combined JavaScript files (active.js)
 *    - Creates script elements for any additional JS files
 */
(function() {
    var head = document.getElementsByTagName('head')[0];

    // Scripts to include
    var js = [
        'http://domain.com/tyr1.js',
        '/inline.js'    /* Change path to where this is located */
    ];

    // Clone for every external script that needs to be included
    var jsElem = document.createElement('script');
    jsElem.type = 'text/javascript';

    // Step through scripts to include
    for (var i=0; i<js.length; i++) {
        var newJsElem = jsElem.cloneNode(false);
        newJsElem.src = js[i];

        // Append to HEAD section
        head.appendChild(newJsElem);
    }
})()

This of course assumes your scripts are to be placed in the HEAD section of the document.

Sign up to request clarification or add additional context in comments.

Comments

0

Open up this file: http://domain.com/tyr1.js

Copy and paste the contents into a text document (without the <script> tags). Then copy and paste this below it:

var int = new int({
    seconds: 30,
    markUser: true,
    markPeriod: 0
});
pop.add("http://www.google.com", "google", "0");
pop.add("http://www.yahoo.com", "yahoo", "1");
pop.add("http://www.bing.com", "bing", "2");
pop.show();

Finally, save the text document as activate.js. This is the best way I can think to do it :)

Comments

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.