NetTalk Central

Author Topic: Script defer  (Read 1018 times)

Jane

  • Sr. Member
  • ****
  • Posts: 349
  • Expert on nothing with opinions on everything.
    • View Profile
    • Email
Script defer
« on: October 23, 2022, 04:08:46 PM »
Bruce,

I ran into a minor head-scratcher updating an app from 11.47 to 12.47.

This is a "hybrid" app. 
Much of it is pure NetTalk.  But for reasons of imposed aesthetic choices, it has a section that displays pages formatted with Bootstrap.  (They are patient status display pages on large monitors on the wall in various clinics.  The status is updated every 15 seconds.   A URL parameter hard-coded into the dedicated ChromeBox at each site tells the server which site's patients to display.)  This was originally built in NT 10 and has been on NT 11 for the past couple of years.

Those pages are a NetWebPage "shell" that contains a framework of scripts and CSS and a named division.
One script is on a timer to update that named division by making a call to another NetWebPage every 15 seconds.
The scripts in the "shell" page are jquery and bootstrap (from the /scripts folder) and several scripts hard-coded in the NetWebPage.  The latter are in the page rather than in a file because they include variables based on the URL sent to the page.

Then kaboom when I rebuilt with 12.47.
The reason turns out to be that you've added "defer" to NetWebServerWorkerBase.AddScript

That "defer" in the jquery loading then caused my locally-built scripts to throw an error - Uncaught ReferenceError: $ is not defined

What do you mean it isn't defined?? I just defined it!!!  (Well, yeah.)

Anyway, now that I'm aware of the issue I've fixed my code in that one NetWebPage and all is well.

But I was wondering whether there would be any value in having an optional parameter to override that "defer" in NetWebServerWorkerBase.AddScript (or an overload version)?

Interestingly, looking at the NT history it says "Add: DEFER attribute added to all <script> sections. " for version 4.31(beta)
And then again "Add: defer attribute to script files" for 12.09.

BTW - you generate
Code: [Select]
defer="defer"
The Mozilla docs say defer is Boolean.  https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-defer

My own experiment finds that just the presence of the single word defer triggers the behavior. 

As that Mozilla link says,
Quote
inline scripts without the type="module" attribute, are fetched and executed immediately,
  So I need to un-defer the jquery rather than defer my inline scripts.

And that's how I spent my Sunday ;)

Cheers,

Jane

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11171
    • View Profile
Re: Script defer
« Reply #1 on: October 23, 2022, 11:48:21 PM »
>> The latter are in the page rather than in a file because they include variables based on the URL sent to the page.

could these not be set as deferred as well?

Cheers
Bruce

Jane

  • Sr. Member
  • ****
  • Posts: 349
  • Expert on nothing with opinions on everything.
    • View Profile
    • Email
Re: Script defer
« Reply #2 on: October 24, 2022, 01:37:58 PM »
>> could these not be set as deferred as well?

Not really.  As the doc I quoted says (emphasis added)
inline scripts without the type="module" attribute, are fetched and executed immediately

The bits I would need to defer I'm actually building up in a StringTheory object with <script> tags and the various parameters built in to the string.  Then sent with packet.Append.

Apparently those are considered "inline" scripts and cannot be deferred.

So I'd have to rewrite them into script files.  And doing javascript functions with $(document).ready that have parameters is a bit above my skill level.  Perhaps an exercise for the student some rainy Saturday.  (Although unfortunately we only get rain for about 12 minutes per year  ::)  )

Simplest for me was just to write a little Clarion code that mimics p_web.AddScript without the DEFER and to call that when I need to.

Apparently this is only an issue for me and nobody else has reported a problem with $(document).ready getting called too early with an inline script.  And I've got a workaround now.

Cheers,

Jane