anchorClickOrder

Check actor click order with form sumit

by abhishekdev

HTML

<form action="/" method="post">
  <input id="samplefield" name="samplefield" value="test" />
  <a href="javascript:proxySubmit('formSubmitBtn')" onclick="return preValidate()">click me</a>
  <input id="formSubmitBtn" type="button" value="submit" class="dontDisplay" onclick="submitClicked()" />
</form>

CSS

.dontDisplay {
  display: none
}

JavaScript

$('#samplefield').on('click.tempiPaaSFix', function(evt) {
  console.log('=> try again to prevalidate events');
  if (preValidate() === false) {
    console.log('cancel events');
    evt.preventDefault();
    evt.stopImmediatePropagation();
  }

});

// Function used for double click problem
function preValidate() {
  console.log('prevalidate', document.readyState);
  var DELAY = 2000; //value of delay in ms
  //checking if a browser supports document.readyState (Firefox < 3.6 + beta included)
  if (typeof document.readyState == 'undefined') {
    var now = +new Date(), //getting current time stamp
      last_time = jQuery(document).data('last_time') || now; //getting last run timestamp or if not found using now as the value

    if ((now - last_time < DELAY) && (now - last_time > 0)) {
      console.log('denied: ' + (now - last_time) + "ms since last run");
      return false;
    } else {
      //storing last run in data variable
      jQuery(document).data('last_time', now);
      console.log(true);
      return true;
    }

  } else {
    console.log('not loading', document.readyState != "loading");
    return (document.readyState != "loading");
  }
}

function proxySubmit(id) {
  var elem = document.getElementById(id);
  console.log('Triggering click on', elem);
  elem && elem.click();
}

function submitClicked() {
  console.log('Submit Clicked');
}



$(document).load(function() {
  // this code is to fix a jQuery issue where anchor elements with javascript
  // in the href causes the onbeforeunload event to fire in IE 8, 9, and 10.
  // The functionality in the href is put into a script and attached to the
  // anchor element, then the href is removed.
  $('a[href^="javascript:"]').each(function(ndx, el) {
    var elm = $(el);
    var h = new Function(elm.attr('href').replace('javascript:', ''));

    // I am adding the name attribute and adjusting the anchor so that if the user
    // is far down the page, the page will not reset to the top because of a...