如何仅将js文件引用到特定的脚本块

How to refer js file to particular script block only

本文关键字:脚本 引用 何仅 js 文件      更新时间:2023-09-26

我遇到一种情况,即我只需要将jqplot.pieRenderer.min.js文件引用到特定的脚本块。因为如果它在顶部全局使用,则会为其他脚本块函数抛出错误。我怎样才能将该js文件维护为特定的脚本块。

我在下面试过。

 <script src="jquery.jqplot.min.js" type="text/javascript"></script> // global js file
 <script type="text/javascript" src="jqplot.pieRenderer.min.js"> //using specific js
        $(document).ready(function () {
             BindLocationStock();
             BindStatusWiseStock();
        });
 <script type="text/javascript"> //using global js 
        $(document).ready(function () {
             BindBarChart();
        });

第一个脚本块可以使用全局 js 文件和块级 js 文件吗?我想要这个场景。我怎样才能做到这一点。请在此处提供帮助。

据我所知,不可能做你想做的事情。

这就是模块模式对 JavaScript 如此有用的原因之一。

我建议重构代码以使用模块模式 - 这样,您就不会有全局变量相互干扰。

我认为这是冲突问题。您可以使用jQuery.noConflict或像这样包装代码:

 <script src="jquery.jqplot.min.js" type="text/javascript"></script> // global js file
 <script type="text/javascript" src="jqplot.pieRenderer.min.js"> //using specific js
  (function ($) {
         BindLocationStock();
         BindStatusWiseStock();
         BindBarChart();
    })(jQuery);