4

I have seen on some websites they have one script tag but call multiple JS file e.g. <script src="script1.js,script2.js,script3.js"></script>

How do I do this? (Looking for both PHP and ASP.NET)

I have done a search on Google but struggled to find anything relevant.

Thanks

2
  • 1
    Any particular reason why you want to do that? Commented Jun 6, 2011 at 15:32
  • Maybe this might help stackoverflow.com/questions/4878571/… Commented Jun 6, 2011 at 15:34

3 Answers 3

6

You need to write a server-side script that parses the script names from the URL or query string, and sends a single response with the contents of all of the scripts.

Make sure to validate the script names to prevent attackers from reading arbitrary files.

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

Comments

2

That loads a single JS file. The URLs just has multiple things that look like filenames in it.

There is a strong probability that the single JS file is built from multiple files by a server side script. If so, that is just a simple matter of…

  1. parsing the filenames from the URL (split on commas)
  2. sanity checking them
  3. concatenating the files into the response

(and giving it a suitable Content-Type header).

It is inefficient to do that for every hit though, so make sure you implement sane caching. Both with HTTP response headers and on your server.

Comments

1

I use:

http://rakaz.nl/code/combine

Just set up something in your .htaccess that points requests to js and css to this file. It handles caching etc

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.