title: How can I make a function execute first before another when it comes last in the DOM?
body: <p>In a software (i.e. OptinMonster), I'm trying to add customized Javascript that adds more functionality to a popup that has a function already written in the software (OptinMonster), in the head tag. I need to make my function execute first but I can't do that since my function is at the bottom of the body tag and the original script and functions in the head tag. How can I overwrite a script in the head tag with the script at the bottom of the body tag, is there a way to make the script at the bottom of the page execute first? </p>
  
  <pre><code>&lt;head&gt; &lt;script src=""&gt;&lt;/script&gt;&lt;/head&gt; //OptinMonster script
  &lt;body&gt;
  &lt;script&gt; //my customised script
  var email = document.getElementById('workMail-field-email');
  var button = document.getElementById('workMail-FieldsElementButton--zfEvw8Dhlz1j5CW5j1H8');
  
  
  email.addEventListener('keydown', validate);
  button.addEventListener('click', stopSubmit);
  
  function validate(){
  
      var inputVal = document.getElementById('workMail-field-email').value;
      var pattern = /^[^ ]+@[me|mac|icloud|gmail|googlemail|hotmail|live|msn|outlook|yahoo|ymail|aol]+\.[a-z]{2,3}$/;
  
      if (inputVal.match(pattern)) {
          document.getElementById("invalid").style.display = "block";
          document.getElementsByClassName('workMail-error-header')[0].style.display = 'none';
          window.email.style.border = "2px solid rgb(255, 153, 153)";
          window.item = true;
      } else {
          document.getElementById("invalid").style.display = "none";
          window.email.style.border = "none";
          window.item = false;
      }
  }
  function stopSubmit(e){
      if (window.item){
          e.preventDefault();
          e.stopImmediatePropagation();
      }
  }
  &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <h2>The function I want to overwrite:</h2>
  
  <pre><code>function(n) {
    var i = n.target,
      o = i.closest(e.listeners.submit) || i;
    o._omns &amp;&amp; o._omns["click.omSubmitForm." + t] &amp;&amp; (n.stopImmediatePropagation(), n.preventDefault(), e.C.Optin.init(), u.trigger(document, "Listeners.submit", {
      type: "default",
      Listeners: e,
      Campaign: e.C
    }))
  }
  </code></pre>
  
  <p>Basically, I want the function not to <strong>validate any email address but only work email addresses</strong> and o<strong>nly then it submits the form</strong>. The problem is that <strong>it submits when it is a normal email not a work email address</strong>.</p>