Search code examples
htmlhttpscriptingwebserversrc

How is the <script> tag handled at the machine level?


I assume the browser contacts the web server and asks for the "src" (in those situations where there's something to src).

Can that interaction be controlled in any way? How much information is available to the server about who wants the src and when?

Alternatively, could one deliberately 404 the request, then control the 404 mechanism to respond appropriately?


Solution

  • Don't mess around with 404 handling for this. If you want to dynamically generate javascript there's no need.

    The browser doesn't care what URL is in the src="" attribute - it certainly doesn't have to end in .js. The important thing is that whatever the URL is, it should return its content with the content-type application/javascript.

    So the simplest thing to do is put the path to a PHP (if that's what you're using) file and just ensure that what's returned is valid Javascript, sent with a correct Content-Type HTTP header