Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  9] [ 4]  / answers: 1 / hits: 24765  / 11 Years ago, mon, april 29, 2013, 12:00:00

In PhP you can.



In html, you sort of can with <script src=...> tag



but what about if we have a .js file and we want to include another .js file. How would we do so?


More From » include

 Answers
120

If you mean in a browser context, not directly, but you can use a loader like at RequireJS (there are several).



Or to do it manually:



var script = document.createElement('script');
script.src = /path/to/the/other/file.js;
document.getElementsByTagName('script')[0].parentNode.appendChild(script);


Note, though,that with the above the functions and such in the other file won't be available for use right away. The code above starts the process of loading the file, but it continues asynchronously. You can use events to know that the load is complete (primarily the load event, but on older versions of IE you have to use onreadystatechange -- this is one reason people use loaders!).


[#78532] Sunday, April 28, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reina

Total Points: 241
Total Questions: 82
Total Answers: 94

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
reina questions
;