简体   繁体   English

如何访问和使用多个JavaScript库?

[英]How can I access and use multiple javascript libraries?

I am new to javascript and programming in general. 我是新来的JavaScript和一般编程人员。 I have been working on a web app that solves simple algebraic equations. 我一直在研究可解决简单代数方程式的网络应用程序。 I am using two libraries, algebra.js and katex.js. 我正在使用两个库algebra.js和katex.js。 How do I use both libraries in my script? 如何在脚本中使用两个库? I would like to keep this as a client-side application. 我想将此保留为客户端应用程序。

I have looked at node.js but my understanding is that node is for server-side development. 我看过node.js,但我的理解是该节点用于服务器端开发。 I have looked at RequireJS but that doesn't seem to handle directories well. 我看过RequireJS,但似乎不能很好地处理目录。 Recently I found Enderjs which seems to use npm and allow for client-side development and still make use of require(). 最近,我发现Enderjs似乎使用npm并允许客户端开发,并且仍然使用require()。

What should I use to make a web app like this? 我应该使用什么来制作这样的Web应用程序? Please let me know if there is anymore information that is needed. 请让我知道是否需要更多信息。

The most basic way to do this is to include multiple script tags at the top of your html file. 最基本的方法是在html文件顶部包含多个脚本标签。 So: 所以:

<head>
    <script src="path/to/library1.js"></script>
    <script src="path/to/library2.js"></script>
    <script src="path/to/my/javascript.js"></script>
</head>
<body>
    <button onclick="myFunction()">Click me</button>
</body>

This will load more than one onto the page. 这将在页面上加载多个。 The order might matter - be wary of which dependencies your chosen libraries have. 顺序可能很重要-请注意所选库具有哪些依赖项。 For example, some will depend on jQuery, so you should load jQuery first then those that depend on it. 例如,有些将依赖jQuery,因此您应该先加载jQuery,然后再加载依赖jQuery的那些。 Order is top down. 订单自上而下。

In my example, if we pretend library2 depends on library1, then the example would work. 在我的示例中,如果我们假装library2依赖于library1,则该示例将起作用。 However if library1 depended on library2, it would not. 但是,如果library1依赖于library2,则不会。

The simplest way is to include the script tags directly in your html file like so (this assumes that you have the algebra.js file in the same folder as your html file): 最简单的方法是像这样直接将脚本标签包括在html文件中(这假设您将algebra.js文件与html文件放在同一文件夹中):

<script src="algebra.js"></script>

If you are loading the library from the internet you have to use the full web path in the src attribute, for example loading the jQuery library from a cdn (content distribution network): 如果要从Internet加载库,则必须使用src属性中的完整Web路径,例如,从cdn(内容分发网络)加载jQuery库:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

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

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