0

Is there a way to structure javascript/jquery by using an include() style of script concatenation similar to the way it can be done server-side in PHP?

I have a very long javascript engine that is currently embedded with dynamic code that ultimately comes from my database.

I want to separate the static js engine code from the dynamic js content code.

Wondering if there is a clever and "easy" way to do that?

0

2 Answers 2

2

You can simply include other javascript files with javascript like this:

document.write('<script type="text/javascript" src="file1.js"></script>');
document.write('<script type="text/javascript" src="file2.js"></script>');

Hope this suits you.

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

4 Comments

Yes, you are correct @nmoliveira. I thought of that too. But actually you triggered me to think of this a bit differently so I now have an idea. Thanks for the tip and helping pull me out of the forest for a moment to see the trees.
Not the right way to structure javascript obviously but, I'm happy if this helped you!
What does document.write have to do with PHP? :-)
@Cypher nothing to do of course. Initially I was thinking about a different approach and left that by mistake. I've edit the answer. Thanks
0

Try require.js or browserify. These provide a way of breaking javascripts into requireable (importable) modules.

2 Comments

I see that browserify is an add-on or plugin. I could not tell right away if require.js is a plug-in or part of the core service @JackAllan
RequireJS is a set of JavaScript functions that allows you to "include" other JavaScript files at runtime.

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.