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?
<head> <script src=""></script></head> //OptinMonster script
<body>
<script> //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();
}
}
</script>
</body>
The function I want to overwrite:
function(n) {
var i = n.target,
o = i.closest(e.listeners.submit) || i;
o._omns && o._omns["click.omSubmitForm." + t] && (n.stopImmediatePropagation(), n.preventDefault(), e.C.Optin.init(), u.trigger(document, "Listeners.submit", {
type: "default",
Listeners: e,
Campaign: e.C
}))
}
Basically, I want the function not to validate any email address but only work email addresses and only then it submits the form. The problem is that it submits when it is a normal email not a work email address.