Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does "this" in onClick work?

I'm new to javascript. I'm curious how does the browser makes an onClick attribute work. You have this in a html:

<button id="1" onClick="reply_click(this.id)">B1</button>

what does a browser do to execute a the (allways javascript?) script function?

I guess this is something like eval-ing the string that is given to the onClick.

But I have doubts like these: What is the applying standard here? How does the browser know which script language to use? How is this filled up by the javascript engine? In what context does the script execute? What happens with a return value? What do I want to read to know what can I rely on when working with these attributes?

Can you help me to understand what's going on?

like image 202
n611x007 Avatar asked Dec 20 '22 22:12

n611x007


2 Answers

When you set an onclick attribute; the browser takes the reference to the node you've set it on, let's call this reference button, and then does the transfomation

onclick="/* code */"

to

button.onclick = function (event) {eval(/* code */);};

When a click event is then tirggered, the function can be thought of as being invoked like this

button.onclick.call(button, click_event);

Giving the onclick the context of button, and hence this is button. It is wrapped in it's own function so if something is vard it will not be set in the global scope.


What happens with a return value?

Returning false is the equivalent of calling event.preventDefault();, exact behaviour is described here.


How does the browser know which script language to use?

Assumes JavaScript unless explicitly stated otherwise


What do I want to read to know what can I rely on when working with these attributes?

Don't learn these HTML attributes, they are dated, can be dangerous and mix your HTML and JavaScript together. Instead learn to use EventTarget.addEventListener

like image 56
Paul S. Avatar answered Dec 22 '22 10:12

Paul S.


Phew, that's a lot of questions!

Q. what does a browser do to execute a the (allways javascript?) script function

Q. How does the browser know which script language to use

A. Modern browsers always assume Javascript, unless it is prefixed with a label, e.g. onclick="vbscript:alert('Hello World!');"

Q. How is this filled up by the javascript engine

A. this always refers to the element on which the onclick was added, in this case the button element

Q. In what context does the script execute

A. The script executes at window scope.

Q. What happens with a return value

A. The return value determines whether or not the browser should continue executing the default code for this element. If the button is a submit button and you return false, your form will not be posted

Q. What do I want to read to know what can I rely on when working with these attributes

A. A Javascript book. Have a look at the top Javascript books on Amazon.

like image 40
CodingIntrigue Avatar answered Dec 22 '22 11:12

CodingIntrigue