0

When a JavaScript code of a web page is implemented, then what happens?

I mean when a browser parses an HTML web page, it comes to the JavaScript code of a web page, then it sends the code to its JavaScript compiler. I don’t understand what the output is of the compilation, and how it has been shown to the J-Component?

If anyone knows about this, please clear my concept.

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
DPD
  • 143
  • 2
  • 8
  • 1
    To my knowledge javascript (which has nothing to do with java) is interperated in most browsers. There is no usable output of the compilation, it is only for the internal use of the browser. Why do you want to know? – Peter Lawrey Nov 23 '10 at 15:36
  • @Krof Java AWT stuff I suppose, I've got no idea what he's asking about though. – Ivo Wetzel Nov 23 '10 at 15:40
  • Your question is very unclear. What do you mean "output of compilation"? JavaScript is an interpreted language; the browser evaluates it on the fly. – Will Vousden Nov 23 '10 at 15:48
  • in your question, you say "java-script", which suggests to me you might be equating Java and JavaScript (correct spelling). They are not related. At all. – Matt Nov 23 '10 at 16:06
  • possible duplicate of [How does browsers handle Javascript](http://stackoverflow.com/questions/4243055/how-does-browsers-handle-javascript) – Paul D. Waite Nov 23 '10 at 16:08
  • 1
    @Peter Lawrey JavaScript has been compiled to bytecode in most browsers basically forever; once IE9 ships it will be JIT-compiled to machine code in all the major browsers. – Jason Orendorff Nov 23 '10 at 16:09
  • I think my idea about the implementation of java-script is not right . I thought that java-script code are compiled but I am wrong . I don't understand that portion "the browser evaluates it on the fly" .Can you describe me the process. – DPD Nov 23 '10 at 16:17
  • Don't any of the given answers suffice? If so, mark an answer as answered or clarify your question, given the answers already present. – Klemen Slavič Dec 09 '10 at 09:35

3 Answers3

3

To answer as tersely as possible, here's what happens:

  1. You request a page.
  2. The server sends you a response containing HTML.
  3. The browser looks for external resources (<script>, <link>, etc.) and starts downloading those.
  4. When it encounters a <script> block, it immediately executes the code contained either within the tag or in the downloaded Javascript file.

This all happens inside the browser; the server only knows about which files were sent down the line to the browser. No other information (including how and if the Javascript code executed in the browser) is available to the server unless specifically transmitted to the server in the form of new requests.

As for Javascript, it's a dynamic language and most browsers handle it using execution within virtual machines that understand bytecode that the interpreter generates when reading the source code. The virtual machine itself then executes the bytecode on the hardware in the appropriate instruction set.

In the case of executing Javascript on a page, the global context is what is passed to the execution engine, so any changes made by the executing Javascript will affect that context; in the case of browsers and web pages, that is the window object.

Klemen Slavič
  • 19,661
  • 3
  • 34
  • 43
2

javascript is not compiled at all, it's interpreted on-the-fly (and it has nothing to do with java).

Maybe read this article to understand the differences

smirkingman
  • 6,167
  • 4
  • 34
  • 47
  • Every JavaScript engine contains a compiler. Firefox, for example, compiles JS to bytecode and then (on all the common platforms) compiles the bytecode to machine code. – Jason Orendorff Nov 23 '10 at 16:11
  • If you define compiling as transforming language A into language B then yes. If you define it as transforming language A into some sort of machine code to be finally executed by HARDWARE, rather than INTERPRETED on-the-fly, then no. – smirkingman Nov 23 '10 at 22:03
1

Just like the browser reads the HTML, on the fly (meaning, as soon as it downloads it) and converts it to something visual, the same with the JavaScript. Except JS, isn't so much visual, as is functional!

Theofanis Pantelides
  • 4,724
  • 7
  • 29
  • 49