1

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.

1
  • I'm not sure I understand. What exactly are you trying to accomplish? Pulling a specific function and nothing else from another JS file? I don't see that being possible unless you want to load it into a server side language like PHP and parse it... otherwise you will just need to pull the entire script file. (Assuming its on the same domain) Commented Aug 15, 2011 at 17:28

3 Answers 3

2
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

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

Comments

1

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

1 Comment

Appending a script tag to the head element will load it in asynchronously, there's no need for ajax.
0

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.

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.