简体   繁体   中英

How to add script to head tag in MkDocs?

I am new to MkDocs and I built up a website using MkDocs and I want to add some script into <head>...</head> .

I tried this code at top of my markdown file but it didn't work:

<head>
<script>
...
</script>
</head>

I found the script would show in <body>...</body> rather than in <head>...</head> .

How to place <script> in <head> tag?

You can use material. in your theme (mkdocs.yml), add a reference to custom dir, and in that dir, you can add a file named main.html which will be included in the "head" part See https://squidfunk.github.io/mkdocs-material/customization/#overriding-partials for more details.

Assuming your are using MkDocs Material as your Theme, inside your mkdocs.yml you can add Entries to the extra_javascript List.

mkdocs.yml :

site_name: "..."
theme: "..."
# ...
extra_javascript:
  - https://cdn.someCdn.someLibrary.js # Online Resource
  - js/ourJsFile.js # Path relative to "docs" Directory
# Works the same way for CSS!
extra_css:
  - css/someCSS.css # Again, relative to the "docs" Directory

For reference: https://squidfunk.github.io/mkdocs-material/customization/#adding-assets

The extrahead placeholder, which should be present in all themes, allows additions to be made to the <head> .

You need to specify a custom_dir in the YAML file and in this directory have a main.html with an extrahead block.

See:

https://www.mkdocs.org/user-guide/customizing-your-theme/#using-the-theme_dir

Try this:

<!DOCTYPE html>
<html>
  <head>
 <script src="https://file.js"></script>
  </head>
  <body>
    {{ content }}
  </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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