Jade:如何包含一个javascript文件

59

我需要在网页中引入一个JavaScript文件。我编写了以下代码:

include /../scripts/jquery.timeago.js

但是我得到

<script>/*
 * timeago: a jQuery plugin, version: 0.8.2 (2010-02-16)
 * @requires jQuery v1.2.3 or later
 *
 * Timeago is a jQuery plugin that makes it easy to support automatically
 * updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
 *
 * For usage and examples, visit:
 * http://timeago.yarp.com/
 *
 * Licensed under the MIT:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Copyright (c) 2008-2010, Ryan McGeary (ryanonjavascript -[at]- mcgeary [*dot*] org)
 */
(function($) {
....
</script>

结果如此。但我需要:

<script src="/Scripts/jquery.timeago.js" type="text/javascript"></script>

如何做?


includes 是用来将一个 Pug 文件的内容包含到另一个文件中的。script 就是你要找的东西。 - TheCrazyProgrammer
4个回答

97
请将以下内容放入jade文件中:

script(src="/Scripts/jquery.timeago.js")


26
别忘了把它放在你的<body>标签结束位置而不是<head>里,以加快页面加载速度。 - Mike Causer

11
你可以将这段代码放入你的jade文件中:
   script(type='text/javascript' src='public/vendor/jquery/jquery.min.js')

10

另外,如果您想在Jade文件中包含内联JS,也可以按照以下方式操作:

script(type="text/javascript").
console.log('hello world');

2

将脚本添加到jade文件的语法是

script(src="",...,otherAttribute="")

例如下面这个示例,它将Jquery Bootstraps添加到页面中。

  script( src="https://code.jquery.com/jquery-3.2.1.slim.min.js",  integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" ,crossorigin="anonymous")

script(src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js", integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q", crossorigin="anonymous")

  script( src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" ,integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl", crossorigin="anonymous") 

仅发布代码有点粗鲁且不太有帮助。您介意解释一下吗?并且可以添加文档链接吗? - nicolallias
非常抱歉,我只是匆忙之中, 在Jade中添加脚本的语法为: script(src="",...,otherAttribute="") 可以添加到HTML页面的任何位置,但上述代码只是一种快速添加Bootstrap Jquery的方法,最好将其添加在闭合body标签之前。 - C Williams

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接