Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can javascript simulate a button click?

I am designing a site where it would be problematic if macros were allowed to run freely.

I have thought of a way to stop a macro made by simulating the HTTP requests from a button click but this would be in vain if they could insert javascript scripts which just "click" the button and proceed as a normal user would.

By simulating a button click, I mean, the button is pressed and the Form the button is in runs with the php code associated with it.

Logic tells me javascript can do this but I would like to know for sure, thank you for any input!

~Andrew

like image 359
AndrewB Avatar asked Jun 28 '13 18:06

AndrewB


People also ask

Can JavaScript click a button?

Javascript has a built-in click() function that can perform a click in code.

How do you trigger a click button?

and a trigger to execute the button1 click event handler. $("#button2"). bind("click", (function () { alert("Button 2 is clicked!"); $("#button1").

How do you simulate a button in click in jest?

To simulate a button click in Jest, we can call the simulate method. to call shallow to mount the Button component. Then we call find with 'button' to find the button element. And then we call simulate with 'click' to simulate a click on it.

What is click method in JavaScript?

Definition and Usage The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.


1 Answers

A button may be always clicked programmatically. For example you may have a page with a form like this:

<form>     <input type="text" />     <button>Do something</button>     <input type="submit"> </form> 

then it is possible just to open debug console and type

document.getElementsByTagName('button')[0].click(); 

which will click the button, or

document.getElementsByTagName('input')[1].click(); 

which will click the submit button of the form, or just

document.forms[0].submit(); 

to submit the form without clicking the button.

There is no way to prevent user from mastering JavaScript code on client. You have to add some validation on server side in order to prevent unwanted user actions.

like image 131
Vadim Avatar answered Oct 14 '22 11:10

Vadim