简体   繁体   English

冲突的JavaScript库

[英]Conflicting javascript libraries

I have seen some questions but non of the answers apply to me. 我看到了一些问题,但没有答案适用于我。

I have two scripts 我有两个脚本

<script src="js/bootstrap.min.js"><script>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>

Depending on which order I place them the code for the other breaks. 根据我放置它们的顺序,其他中断的代码。 When I look at the source code I see its because the function names conflict. 当我查看源代码时,会看到它,因为函数名称冲突。 I need to load both. 我需要同时加载两者。

Can I somehow make the entire second library load into a separate namespace? 我可以以某种方式使整个第二个库加载到一个单独的命名空间中吗? How do I go about making all the functions load from both scripts :/ 我该如何从这两个脚本加载所有功能:/

What I'm trying to do I need to call prettyPrint() from run_prettify.js 我要执行的操作需要从run_prettify.js调用prettyPrint()

what prettyPrint() does is insert <ol> 's and <li> 's and <span> 's and into my tag which contains a chunk of code. prettyPrint()作用是将<ol><li><span>插入到我的包含大量代码的标记中。 Then once those tags are inserting my custom css can style the chunk of code. 然后,一旦这些标签插入,我的自定义CSS就可以设置代码块的样式。

Have a look at RequireJS http://requirejs.org/ . 看看RequireJS http://requirejs.org/ It is a library designed to build javascript modules which are completely self contained and manages dependencies for you as well. 它是一个库,旨在构建完全独立的javascript模块,并为您管理依赖项。

Using RequireJS you could do something like: 使用RequireJS,您可以执行以下操作:

require(["js/bootstrap.min"], function(bootstrap) {
    //use bootstrap here
});

require(["https://google-code-prettify.googlecode.com/svn/loader/run_prettify"], function(run_prettify) {
    //use run_prettify here
});

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

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