简体   繁体   English

直接从.js或通过.php加载javascript源代码

[英]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:直接从.js文件或通过.php加载javascript代码有什么区别,如下例:

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

The $_GET['id'] will tell the php to load script id = 1 (script1.js) $_GET['id'] 将告诉 php 加载脚本 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.提前致谢。

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.您希望通过 php 脚本路由您的 js 的唯一原因是,如果您出于某种原因动态生成或修改 javascript。 Otherwise it makes much more sense to link the js file directly.否则直接链接js文件更有意义。 This will allow your web server to handle the request as a static file rather than bottlenecking it through PHP.这将允许您的 web 服务器将请求作为 static 文件处理,而不是通过 PHP 对其进行瓶颈处理。

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.没有要加载的 PHP 解释器,没有任何逻辑,只需下载文件。

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.第一个示例的一个可能原因是唯一的动态生成的 JS,或者通过在 JS 为 output 之前发生的一些额外验证来防止直接访问 JS 源。

You can try replacing your line with something like this:您可以尝试用以下内容替换您的线路:

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

It can be place anywhere.它可以放在任何地方。

in case of loading from js file, its almost static codes or plugins that do not change per request but in case of loading from php file with param we can change the response or file content easily just like as in php files如果从 js 文件加载,其几乎 static 代码或插件不会根据请求更改,但如果从带有参数的 php 文件加载,我们可以像在 ZE1BFD7623621E409CEE4AC01 文件中一样轻松更改响应或文件内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM