Javascript doesn't seem to work on my Obsidian Publish site

What I’m trying to do

I have been trying to get Javascript working on my site, primarily to use the sample script in this post to be able to show the created date and tags of my notes in the properties.

Things I have tried

I set up a custom domain, since the documentation says the site must be on a custom domain for javascript to work.

I created the publish.js file, and whenever I change it, the publish settings sees the changes and suggests the update. I can see in the Network tab of the devtools that the publish.js file is there.

I tried working with Chat GPT to test if Javascript is working, by attempting to place a message in the console, but that message never showed up.

I tried pasting every version of the javascript that worked for other users from that properties post, but none of it appeared to affect the site at all. The CSS file does affect the site though.

I have never worked with javascript before, so I don’t know if I’m doing something wrong, but I worked extensively with ChatGPT to atttempt to troubleshoot, yet nothing worked.

At this point, I suspect that custom javascript isn’t enabled on my site somehow.

Here’s my site: The Quest - Inner Voice Life

Can you please paste it here, and show it exactly as you used it?

Put it between ``` triple backticks so it formats as code. And if you already have backticks in the code you copied, that might be the problem. It might have copied the code formatting from the forum.

```
Paste the code you used here. Or show a screenshot.
And if you pasted code that had these backticks into your site,
you probably need to remove that.
```

(Edit: Oh, I can see your publish.js directly. I see there are no problematic backticks in that file. But maybe in the code you’re trying to run?)

1 Like

This is the code Chat GPT originally supplied to test it by creating a console log. But it never created any console log nor affected the paragraph content.

document.addEventListener('publish:loaded', () => {
  console.log('JavaScript is running in Publish');
  const paragraph = document.querySelector('p');
  if (paragraph) {
    paragraph.textContent = 'JavaScript is working in Publish!';
  }
});

I also did test the exact same code on a very simple HTML page that I opened in Chrome, and it did change the paragraph text, and generate the console log message.

I also already asked Obsidian tech support, and they said ""Standard Javascript won’t work unless its properly scoped to Publish. You’ll want to have ChatGPT read this file so it better understands how to interface and build code for Publish. obsidian-api/publish.d.ts at master · obsidianmd/obsidian-api · GitHub "

I don’t understand that file, and sharing that with Chat GPT didn’t do anything, so that’s when I tried the existing Javascript already in use by others in this forum on their publish sites, and that still didn’t work either.

Yeah, looks like you got some errors higher up. You can see this for yourself by pressing CTRL/CMD + SHIFT + i on your keyboard and going to “console”. That is where you can see the console logs (in case you weren’t aware).

1 Like

I reverted to the original code from the properties post that just adds the created date, and I noticed that it specified “created” in lowercase in addition to “updated”, whereas my property is “Created” with an uppercase, and I don’t have an “updated” property on most of my pages. So I removed the references to “updated” from the code, and change to uppercase “Created”, and the created date now shows!

However, I have to refresh each page individually for the date to show, and I wasn’t able to combine the tags and the created date (yet at least), but at least now I have some javascript working and functional enough for now!

Thanks for the help.