Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

load javascript source code direct from .js or through .php

What is the difference between loading javascript code direct from .js file or through .php like the following example :

<script type="text/javascript" src="myscript.php?id=1"></script>

The $_GET['id'] will tell the php to load script id = 1 (script1.js)

OR

<script type="text/javascript" src="script1.js"></script>

what is the fastest / efficient / safe way between those two method above

Thanks in advance.

like image 606
froditus Avatar asked Jul 27 '26 14:07

froditus


2 Answers

The only reason why you would want to route your js through a php script is if you are, for some reason, dynamically generating or modifying the javascript. Otherwise it makes much more sense to link the js file directly. This will allow your web server to handle the request as a static file rather than bottlenecking it through PHP.

like image 139
Dan Simon Avatar answered Jul 29 '26 06:07

Dan Simon


Obviously it's faster to load it straight through with the second example. No PHP interpreter to load, no logic whatsoever, just downloads the file.

One possible reason for the first example is unique dynamically generated JS, or to prevent direct access to the JS source by some additional verification that occurs before the JS is output.

like image 30
Fosco Avatar answered Jul 29 '26 06:07

Fosco