如何在服务器端包含 javascript 文件?

发布于 2024-12-09 16:37:30 字数 92 浏览 3 评论 0原文

我想在服务器端将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

給妳壹絲溫柔 2024-12-16 16:37:30

没有内置方法可以从 JavaScript 文件包含其他 JavaScript 文件...但您可以将它们添加到 DOM 中 ->

function addJavascript(jsname,pos) {
  var th = document.getElementsByTagName(pos)[0];
  var s = document.createElement('script');
  s.setAttribute('type','text/javascript');
  s.setAttribute('src',jsname);
  th.appendChild(s);
}

用法示例:

addJavascript('newExternal.js','body');

There is no built in way to include other JavaScript files from a JavaScript file ... but you can add them into the DOM ->

function addJavascript(jsname,pos) {
  var th = document.getElementsByTagName(pos)[0];
  var s = document.createElement('script');
  s.setAttribute('type','text/javascript');
  s.setAttribute('src',jsname);
  th.appendChild(s);
}

Example usage :

addJavascript('newExternal.js','body');
回忆追雨的时光 2024-12-16 16:37:30

正如 Supercharging javascript 文章建议的那样 - 带有PHP,您可以读取所有 .js 文件并创建一个大的 js 文件,然后将其整体发送。

这是文章中示例的简化,只是为了帮助您入门:

<?php 
define('SCRIPT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/script/');

$files = array(
    'jquery.js',
    'myLib.js',
    'site.js'
);

header('Content-Type: text/javascript');
foreach (files as $file) {
    if (@readfile(SCRIPT_DIR . $file) === false) {
        error_log("javascript.php: Error reading file '$file'");
    }
}
?>

我还建议阅读全文 在 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:

<?php 
define('SCRIPT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/script/');

$files = array(
    'jquery.js',
    'myLib.js',
    'site.js'
);

header('Content-Type: text/javascript');
foreach (files as $file) {
    if (@readfile(SCRIPT_DIR . $file) === false) {
        error_log("javascript.php: Error reading file '$file'");
    }
}
?>

I would also recommend to read the full article Supercharging JavaScript in PHP to get more info.

怎会甘心 2024-12-16 16:37:30

JSfile = "1.js, 2.js, 3.js"

for 循环

document.write();

JSfile = "1.js, 2.js, 3.js"

for loop it

document.write();

秋凉 2024-12-16 16:37:30

您必须使用脚本加载器,我想推荐在 Twitter 工作的达斯汀·迪亚兹 (Dustin Diaz) 开发的 script.jsGithub 上的项目页面
这是你如何使用它
假设您有一个要在页面中加载的 jquery 插件,这里是您应该做的

// load jquery and plugin at the same time. name it 'bundle'
$script(['jquery.js', 'my-jquery-plugin.js'], 'bundle')

// load your usage
$script('my-app-that-uses-plugin.js')


/*--- in my-jquery-plugin.js ---*/
$script.ready('bundle', function() {
  // jquery & plugin (this file) are both ready
  // plugin code...
})


/*--- in my-app-that-uses-plugin.js ---*/
$script.ready('bundle', function() {
  // use your plugin :)
})

示例也来自项目页面。以下是您应该遵循的步骤

  1. 首先加载 Script.js 文件
  2. 现在包括异步加载所有其他所需脚本的依赖项加载器脚本[依赖项加载器脚本的内容将类似于上面]

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

// load jquery and plugin at the same time. name it 'bundle'
$script(['jquery.js', 'my-jquery-plugin.js'], 'bundle')

// load your usage
$script('my-app-that-uses-plugin.js')


/*--- in my-jquery-plugin.js ---*/
$script.ready('bundle', function() {
  // jquery & plugin (this file) are both ready
  // plugin code...
})


/*--- in my-app-that-uses-plugin.js ---*/
$script.ready('bundle', function() {
  // use your plugin :)
})

example is also from project page. Here are steps you should follow

  1. Load the Script.js file first
  2. Now include the dependency loader script that loads all the other wanted scripts async[the content of dependency loader script would be like above]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文