Sam
Sam

Reputation: 281

Reference JS file in JS file

I have a Js function that I would like to:

  1. Reference another js file
  2. Pull a function out.

I would like to do this JS side and not reference on the actual page as I need this process to happen dynamically.

Upvotes: 1

Views: 958

Answers (3)

Alex Turpin
Alex Turpin

Reputation: 47776

If you're working with the browser, jQuery has an helper function for it, $.getScript.

Upvotes: 1

shawndumas
shawndumas

Reputation: 1413

var h = document.getElementsByTagName('head')[0],
    s = document.createElement('script');

s.type = 'text/javascript';
s.onload = function () { document.getElementById('hello').innerText = h.innerText; };
s.src = 'http://html5shiv.googlecode.com/svn/trunk/html5.js';
h.appendChild(s);

see: http://jsbin.com/uhoger

Upvotes: 2

Tejs
Tejs

Reputation: 41246

The only option I can think of is to dynamically insert a new script tag into the page targeting your desired script from your initial javascript. Just have your initial script insert the new <script> tag on load, or upon request and then test for availability.

Upvotes: 0

Related Questions