Java Scripts in Drafts (or elsewhere)

I am actively developing some scripts and actions in Drafts that use the HTML Preview Action.

The underling topic - how to set it up and a good HTML document should be created and a great reply by @chrillek - brought me to the following article:

EventTarget.addEventListener() - Web API Referenz | MDN

I had to read with some astonishment that the practice of many tutorials to put Java Script inside the body of the HTML document is really bad practice and might even be the reason for some of the things I hate so much in web-apps or on webpages.

As noted in my article

I am looking for a good and compact way to facilitate HTML Preview with my different ideas. The goal would be two fold:

  1. Generate good and nice HTML documents (with much Java Script)
  2. Have simple and compact templates in the Draft action (with template tags)

Take away

As I read the article about the EventListener the practice of Java Script in the body could be completely abandoned by using the load event.

During generation the HTML Preview is not very talkative if a parse/specific error occurs. The EventListener error event might be a nice way to give the developer more details on the reason of the script.

Why do they have to have “much JavaScript”? Not that I’m against JS, but I think it should be necessary for a certain purpose. So not necessarily “much” but “the JS required for this document”. Eg, prism.js for code highlighting – but jQuery?

It would be difficult to add an onload listener without also adding JavaScript, me thinks. Though I don’t want to curb you enthusiasm, any JS manipulating the DOM is using this event and a listener for it since ages :wink:
Again: I suggest to talk about the effect(s) you want to achieve with JS.

I do not want to abandon Java script I want to abandon it inside the body element.

The idea would be to add it in the head and register the onload event listener.

In my JQuery days there where no references to that event listener. Everything was achived by putting the Java Script behind the document in the body.

Am I wrong that this is bad HTML code?

For example:

Or even to please the VIM users with vim.js :cold_face:

JQuery was bloatware and considered a bad example by many JS aficionados.

It is a performance bottleneck, especially with mobile devices. Not necessarily bad code. In the context of Drafts, it might not be an issue at all, since JS is probably only used to get a nice printout. As in your example about nomnoml and mine about code highlighting.

1 Like