Download javascript source code directly from .js or via .php

What is the difference between loading javascript code directly from a .js file or via .php, as in the following example:

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

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

OR

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

what is the fastest / most effective / safest way between the two methods above

Thanks in advance.

+3
source share
4 answers

The only reason you want to redirect js through a php script is because you for some reason dynamically generate or modify javascript. Otherwise, it makes sense to link the js file directly. This will allow the web server to process the request as a static file, rather than narrowly profile it through PHP.

+3
source

, . PHP- , - , .

JS JS , JS.

+1

in case of loading from a js file, its almost static codes or plugins that do not change per request , but in case of loading from a php file with a parameter, we can change the answer> or the file contents just in the same way as in php files

0
source

You can try replacing the string as follows:

   <?php
   echo("<script type=\"text/javascript\">\n");
   require("myscript.php?id=1");
   echo("</script>\n");
   ?>

It could be a place anywhere.

0
source

All Articles