Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, HTML, Javascript execution order

I have a test.php file and this file contains some PHP code, HTML elements and some internal JavaScript and some external JavaScript include.

I want to know which is first to load or execute.

PHP or HTML or JavaScript? I want to know execution order.

Your answers are greatly appreciated and very helpful to me and others also.

like image 569
prakash Avatar asked Jun 04 '12 06:06

prakash


People also ask

In what order does JavaScript execute code?

JavaScript is synchronous. This means that it will execute your code block by order after hoisting. Before the code executes, var and function declarations are “hoisted” to the top of their scope.

Are script tags executed in order?

Script tags are executed in the order they appear It also means scripts which appear later on the page can depend on things scripts which appear earlier have done. Elements on the page won't render until all the script tags preceding them have loaded and executed.

How do browsers load scripts?

In order to assess the consequences of any such decision, it helps to understand how browsers work: When the browser processes an HTML document, it does so from top to bottom. Upon encountering a <script> tag, it halts (“blocks”) further processing[2] in order to download the referenced script file.


2 Answers

Pragmatically speaking, this is the typical order:

  • PHP runs first and constructs the page.
  • The browser loads the resulting HTML (any JavaScript found gets executed immediately)
  • Any JavaScript that was tied to the DOM ready or load event gets executed once the whole HTML is read and all objects are loaded respectively.
like image 135
Ja͢ck Avatar answered Sep 22 '22 11:09

Ja͢ck


PHP will execute first, then HTML and finally javascript.

  • You send request to server, server executes your script
  • Then returns rendered html to browser, browser parses HTML(inline javascript executed)
  • Finally executes external included javascript files, one by one in order they are included.
like image 28
Aurimas Ličkus Avatar answered Sep 19 '22 11:09

Aurimas Ličkus