Jump to content

Is it possible to set "defer", "async", "crossorigin" on Script in head ?


Recommended Posts

Posted

Hi, 

I'm trying to do some loading optimizations and I want to apply some of the methods described on this link.

https://www.geeksforgeeks.org/javascript/dom-loading-issues-with-javascript/

1. all MainForm.Script

2.all unigui js files like 

<script src="/ext-7.4.0/build/classic/theme-neptune/theme-neptune.js"></script>  

<script src="/ext-7.4.0/build/ext-all.js"></script>

and all other or use pre like  this example:

<link rel="preload" href="important-js.js" as="script" />

https://developer.mozilla.org/en-US/docs/Learn_web_development/Extensions/Performance/JavaScript

 

3. all scripts loaded from file

like that : <script src="/MyCustomfiles/MyCustom.js" defer=""></script>

to set defer to script on point 3 I use:     UniAddJSLibrary('/MyCustomfiles/MyCustom.js', False, [upoPlatformBoth, upoDefer]);//upoAsync]); on TUniServerModule.UniGUIServerModuleCreate.

 

How to apply to point 1 and point 2 ?

Solutions to Fix DOM Loading Issues:

  • Place Your Scripts at the End of the Body: This is the most common solution. By placing your JavaScript at the end of the <body>, it ensures the DOM is fully loaded before any scripts are executed.
 
<body>
    <!-- HTML content here -->
    <script src="your-script.js"></script>
</body>
 
  • Use defer or async Attribute in Script Tags: If you need to include your script in the <head> section, use the defer attribute. This ensures that the script is executed only after the HTML document has been completely parsed.
 
<head>
    <script src="your-script.js" defer></script>
</head>
 
  • Alternatively, use async if you want the script to load in parallel with HTML parsing but don’t care when it executes (not recommended for DOM manipulation).
 
<script src="your-script.js" async></script>
  • 3 months later...
Posted

So, no answer!

But new question is here:

Is it possible (and how) to add async or crossorigin when add user script ?

like example<script src="https://connect.facebook.net/en_US/sdk.js?hash=5810.....b60b045e3.......4" async="" crossorigin="anonymous"></script>

  • irigsoft changed the title to Is it possible to set "defer", "async", "crossorigin" on Script in head ?
Posted
6 hours ago, irigsoft said:

So, no answer!

But new question is here:

Is it possible (and how) to add async or crossorigin when add user script ?

like example<script src="https://connect.facebook.net/en_US/sdk.js?hash=5810.....b60b045e3.......4" async="" crossorigin="anonymous"></script>

You have 2 options
either use servermodule.custommeta to add your script directly like "<script src="your-script.js" defer></script>"

or use UniAddJSLibrary('YOUR_JS_URL', True, [upoPlatformBoth, upoAsync, upoDefer]);

  • Thanks 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...