423,682 Members | 1,348 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,682 IT Pros & Developers. It's quick & easy.

simulate form submit (why does it reload the page?)

P: 1
In short:

Execute this js script on https://skribbl.io (via your browser console)

Problem: it "switches"/reloads the page after successfully joining

Expand|Select|Wrap|Line Numbers
  1. this.Enterrr = document.getElementById('formLogin');
  2.  
  3. let ev = document.createEvent('Event');
  4.  
  5. ev.initEvent('submit');
  6.  
  7. this.Enterrr.dispatchEvent(ev);
Who can change the code so the switching/reloading does not appear?


Even easier code:
Expand|Select|Wrap|Line Numbers
  1. var foo = document.getElementById('formLogin');
  2. foo.dispatchEvent(new Event('submit'));
Also reloads the page... And simply clicking manually or pressing enter does not lead to a page reload. Weird...
Jul 3 '18 #1
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,179
well - the question is more - what are you trying to achieve. to prevent the default submit behaviour you can use:

event.preventDefault()

but in this case the form wont be submitted - thus making it obsolet to even fire a submit-event. so as i said - some more information and may be posting the html-form would be helpful for a useful answer. if the form has a real submit button it will reload - its easy to see in chrome's dev-tools - so i doubt that the submit from the submit button behaves different from the programmatic submit. this can only happen if there is other code that might cause this or its not a real submit button.
Jul 6 '18 #2

Post your reply

Sign in to post your reply or Sign up for a free account.