Can javascript simulate a button click?

前端 未结 2 990
栀梦
栀梦 2020-12-13 18:41

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 request

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 19:21

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

    than it is possible just to open debug cosole 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.

提交回复
热议问题