How to create dynamic external javascript files?
Asked Answered
A

3

6

I am thinking about how some online services create dynamic JavaScript files. These files have the .js extension, but their content is not static. I found a sample file here. It seems that this script is generated with a higher level programming language. I think it is done with PHP or something similar, but I am not sure, and I have not found any documentation about this topic.

Is there any well known way to create these kind of dynamic JavaScript files?

Anstus answered 17/5, 2013 at 15:21 Comment(2)
you can create them with any server-side language. the file extension does not have to be js, or anything really, content-type is ignored by <script> tags. in other words, it's just a web page without the HTML tags...Fogel
The same way you create dynamic-anything-elseBarrier
O
10

Consider carefully whether generating a dynamic JS file is necessary at all. Instead of generating dynamic JS, you can often simply inject static script(s) and use separate JSON to support dynamic configuration into your page.

If you view source on this (or about any) StackOverflow page you'll see that they're using this same pattern: Static external .js files that reference a separate centralized chunk of JSON for configuration. That JSON is what provides dynamism.

View source and look for this:

StackExchange.init({...

Most server side languages make it trivial to serialize an object to JSON so you can inject it into your page.

Here's ten reasons utilizing external static js files is preferable:

  1. Cached
  2. Code colored
  3. Syntax checked
  4. Separation of concerns
  5. Reusable
  6. Easier to read.
  7. One less layer of abstraction
  8. Can serve minified and obfuscated
  9. Avoids string parsing on every request
  10. StackOverflow and all the cool kids are doing it (hey, I promised 10 reasons.)

More info here: http://www.bitnative.com/2013/10/06/javascript-configuration-object-pattern/

Oleaceous answered 17/5, 2013 at 15:36 Comment(2)
Are there few other examples or an established pattern or framework to do this for a large project?Australian
I'm not aware of any off-hand, but one tip: If you find the injected JSON getting too large, it's likely a sign the page is doing too much. Consider creating two separate pages with more limited responsibilities.Oleaceous
V
1

That depends on whether you want to generate files or return data. Generating files would be done with something like file_put_contents. Alternatively you could have a .js file in a folder with a .htaccess file that tells you to execute it as php, allowing you to simply generate the script on the fly based on session, get, or post parameters.

Vermilion answered 17/5, 2013 at 15:27 Comment(0)
W
1

You can use any server-side language to create dynamic javascript files, javascript files don't need to end with .js. If you really want your files to end with .js you'll need to edit your server settings to also process .js files as for instance PHP files.

You can also use server code to generate inline javascript.

But be careful when generating javascript files, it can become very complex when you are mixing two programming languages

Westsouthwest answered 17/5, 2013 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.