How to use $document
in custom js?
#6954
-
I'm trying to follow the doc here and I'm not able to get the following to work: document$.subscribe(function() {
console.log("Initialize third-party libraries here")
}) I've tried both including a script with
Am I missing something about how to use this? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Hello @naterichman, As for further debugging tips, the code block you provided is supposed to go into your extra.js file. https://squidfunk.github.io/mkdocs-material/customization/#additional-javascript mkdocs-material/src/templates/base.html Lines 433 to 441 in 92b3e1b mkdocs-material/src/templates/assets/javascripts/bundle.ts Lines 308 to 318 in 92b3e1b |
Beta Was this translation helpful? Give feedback.
-
You cannot use document.addEventListener("DOMContentLoaded", () => {
// Your code
}) |
Beta Was this translation helpful? Give feedback.
-
Sorry for the delay, here is a reproduction The additional file method is alright, but I'd like to not have to include my additional JS if I only want it applied on 1 or two pages out of 10s or 100s. I was basing what I was doing out of this issue which mentioned including a I realize now that is not possible, so my question is, even though the event listener for |
Beta Was this translation helpful? Give feedback.
You cannot use
document$
inside Markdown inscript
tags, because it is only available at the end of the rendered page. This is because the JavaScript is initialized at the bottom of the body element (which is a best practice to improve rendering times). A better idea is to move your JavaScript into an additional file, which ensures correct order. Alternatively, you can use the following in your Markdown file (not recommended for third-party libraries):