如何在服务器端包含 javascript 文件?
我想在服务器端将 JS 文件包含在另一个 JS 文件中,以防止复制粘贴和从客户端加载多个 JS 文件。 就像 PHP 所做的那样 include('file.js') 。
I want to include JS file inside another JS file, on server side, to prevent copy-paste and multiple JS files loading from client.
Just like include('file.js') as PHP would do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有内置方法可以从 JavaScript 文件包含其他 JavaScript 文件...但您可以将它们添加到 DOM 中 ->
用法示例:
There is no built in way to include other JavaScript files from a JavaScript file ... but you can add them into the DOM ->
Example usage :
正如 Supercharging javascript 文章建议的那样 - 带有PHP,您可以读取所有 .js 文件并创建一个大的 js 文件,然后将其整体发送。
这是文章中示例的简化,只是为了帮助您入门:
我还建议阅读全文 在 PHP 中增强 JavaScript 以获取更多信息。
As Supercharging javascript article suggest - with PHP you can read all your .js files and create one big js file that you will send in one piece.
This is a simplification of the example from the article, just to get you started:
I would also recommend to read the full article Supercharging JavaScript in PHP to get more info.
JSfile = "1.js, 2.js, 3.js"
for 循环
document.write();
JSfile = "1.js, 2.js, 3.js"
for loop it
document.write();
您必须使用脚本加载器,我想推荐在 Twitter 工作的达斯汀·迪亚兹 (Dustin Diaz) 开发的
script.js
。 Github 上的项目页面。这是你如何使用它
假设您有一个要在页面中加载的 jquery 插件,这里是您应该做的
示例也来自项目页面。以下是您应该遵循的步骤
You will have to use a script loader and i would like to recommend
script.js
by Dustin Diaz who works at Twitter. Project Page on Github.Here is how you use it
assuming you have a jquery plugin to be loaded in a page here is what you should do
example is also from project page. Here are steps you should follow