Load JS before rendering the page

Asked

Viewed 355 times

-2

Good afternoon,

I have a code for php where it has a list of publications and it uses the Ajax, but unfortunately it is being the last to be downloaded by the page.

After all the files you have inside the while, and is causing the Ajax does not work properly..

Doubt: There is a way to import JS before any load?

I tried for the tag Defer and Async of HTML5 but it didn’t work.

  • Put your HTML code and where you’re referencing the code so we can help you

  • If you don’t want the answer to be asynchronous, why use AJAX? Why don’t you deliver all the data in the HTTP response with PHP?

  • Good reading https://css-tricks.com/prefetching-preloading-prebrowsing/ but browse other sources!

  • @The code is too big to put here, I tried to load asynchronous, but no difference http://prntscr.com/m5g24c

  • Without the code, there’s no helping you, buddy...

2 answers

1


Hello,

I believe that a good approach together with the javascript call right after the Head tag is to use in your JS file an IIFE (Immediately Invoked Function Expression) structure, This way you will be self-performing your function. More information can be seen here: https://developer.mozilla.org/en-US/docs/Glossario/IIFE

0

Just put the Javascript code or its link right after the Head tag.

The page always expects the script to be loaded and checked before continuing to load all other elements below it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.