Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<form action="javascript:alert(this);"> what is "this"?

Tags:

javascript

When trying to debug what is being submitted, I wrote this.

<form action="javascript:alert(this);"
  1. Is it possible to alert what is being submitted?
  2. Here "this" denotes what?

I got object in alert box & unable to decide anything out of it. :-)

like image 892
rajakvk Avatar asked Sep 29 '09 05:09

rajakvk


People also ask

What is form action in JavaScript?

form action javascript. In an HTML form, the action attribute is used to indicate where the form's data is sent to when it is submitted. The value of this can be set when the form is created, but at times you might want to set it dynamically.

How do you alert a form in JavaScript?

The JavaScript alert() function is a function available on the global window object. It commands the browser to display a modal dialog with a message and an "OK" button.

Why do we use alert box in JavaScript?

An alert dialog box is mostly used to give a warning message to the users. For example, if one input field requires to enter some text but the user does not provide any input, then as a part of validation, you can use an alert box to give a warning message.

What is form action tag?

The HTML form action attribute defines where to send the form data when a form is submitted in an HTML document.


1 Answers

In your example this is the global window object. Try it yourself:

<form action="javascript:alert(typeof this.setTimeout);">

results in "function" (i.e. the global function). Or try:

<form action="javascript:alert(this.nodeType);">

results in undefined (i.e. it's not pointing to the form element)*.

The value of "this" inside an attribute will only ever be one of two things:

  1. the global window object
  2. the element itself

The only time this points to the element itself is when it is used inside an intrinsic event attribute (the ones that are prefixed with "on", eg "onclick", "onload", etc). These attributes are special: the browser re-scopes this to the element the event is firing on, and creates the event object (with that name) also available inside the attribute.

If the attribute is not one of the intrinsic events, "this" will be the global window object.


Footnotes:

* unless of course you happened to have a global var named "nodeType"

like image 101
Crescent Fresh Avatar answered Oct 04 '22 15:10

Crescent Fresh