Dave's Brain

Browse - Programming Tips - How can I do a C-like #include in javaScript?

Date: 2009oct15
Language: javaScript
Library: jQuery

Q.  How can I do a C-like #include in javaScript?

A.  There is no equivalent to a #include is javaScript.
But you can use Ajax to pull in a .js file and then run it!

Here's how to do that with jQuery:

$.ajax({ type: 'GET', url: 'my_script.js', dataType: 'script' });
Or even simpler:
$.getScript(url, callback);
You can do this in pure javaScript too. Search for "Ajax" here for more info.

Add a comment

Sign in to add a comment
Copyright © 2008-2018, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.