Jade: How to include a javascript file

ExpressPug

Express Problem Overview


I need to include a javascript file to webpage. I write the following:

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

but I get

<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>

as result. But I need:

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

How to do it?

Express Solutions


Solution 1 - Express

Put this in your jade file:

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

Solution 2 - Express

You can put this code in your jade file:

   script(type='text/javascript' src='public/vendor/jquery/jquery.min.js')

Solution 3 - Express

Also, if you would like to include inline js within your jade file you could do the following as well:

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

Solution 4 - Express

Syntax for Adding scripts to your jade files is

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

example like the one below, that adds Jquery Bootstraps to the page.

  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") 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOleg ShView Question on Stackoverflow
Solution 1 - ExpressEhevuTovView Answer on Stackoverflow
Solution 2 - ExpressAnubhav SinghView Answer on Stackoverflow
Solution 3 - ExpressLawrence ChangView Answer on Stackoverflow
Solution 4 - ExpressC WilliamsView Answer on Stackoverflow