简体   繁体   English

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

[英]How to refer js file to particular script block only

I have a situation where I need to refer a jqplot.pieRenderer.min.js file to a particular script block only. 我有一种情况,我只需要将jqplot.pieRenderer.min.js文件引用到特定的脚本块。 Because if it is used globally at the top, it is throwing error for the other scripts block functions. 因为如果在顶部全局使用它,则其他脚本块功能将抛出错误。 How can i maintain that js file to particular script block. 我该如何将该js文件维护为特定的脚本块。

I tried below. 我在下面尝试过。

 <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();
        });

Can first script block use global js file and block level js file? 第一个脚本块可以使用全局js文件和块级js文件吗? I want this scenario. 我想要这种情况。 How can I achieve this. 我该如何实现。 Please assist here. 请在这里协助。

As far as I know, it is not possible to do what you are trying to do. 据我所知,不可能做您想做的事情。

This is one of the reasons why the module pattern is so useful for JavaScript. 这是模块模式对JavaScript如此有用的原因之一。

I recommend refactoring your code to use the module pattern - that way, you won't have global variables interfering with one another. 我建议将代码重构为使用模块模式 -这样,您就不会有全局变量相互干扰。

I think it's conflict issue. 我认为这是冲突问题。 Either you can use jQuery.noConflict or wrap up your code like this: 您可以使用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);

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

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