I have a JS script embedded in another website. the target is to gram field contact and post it. my problem is that the script runs well on safari browser but not with chrome browser.
I tried: -clear cache on browsers
also tried to put alert to see the script is running. works of safari and do not show the alert in chrome even in incognito mode
here is the code:
var API_KEY = "key";
var API_DOMAIN = location.hostname;
var TOKEN = getParameterByName('token');
console.log('params');
console.log(API_DOMAIN);
console.log(TOKEN);
var reg_emailInputId = document.getElementById('reg_email');
// on keyup, start the countdown
reg_emailInputId.onfocusout = function() {
// init
var isValid = validateEmail(reg_emailInputId.value);
// check if token is not empty
if ((TOKEN !== '' || TOKEN !== null) && isValid === true) {
doneTyping(reg_emailInputId.value, '107');
}
console.log('listener_on');
};
function doneTyping(myInputValue, id) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(xhttp.responseText);
console.log('send params');
}
};
xhttp.open("POST", "domain", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(params);
}
function getParameterByName(name, url = window.location.href) {
name = name.replace(/[\[\]]/g, '\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
function validateEmail(email) {
// init
var mailformat = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
// check
if (email.match(mailformat)) {
return true;
} else {
return false;
}
}
<input type="text" id="reg_email" >
reg_emailInputId.onfocusout = function() { is not triggered in Chrome
Use an eventListener
var API_KEY = "key";
var API_DOMAIN = location.hostname;
var TOKEN = getParameterByName('token');
console.log('params');
console.log(API_DOMAIN);
console.log(TOKEN);
var reg_emailInputId = document.getElementById('reg_email');
// on keyup, start the countdown
reg_emailInputId.addEventListener("focusout", function() {
// init
var isValid = validateEmail(reg_emailInputId.value);
// check if token is not empty
if ((TOKEN !== '' || TOKEN !== null) && isValid === true) {
doneTyping(reg_emailInputId.value, '107');
}
console.log('listener_on');
});
function doneTyping(myInputValue, id) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(xhttp.responseText);
console.log('send params');
}
};
xhttp.open("POST", "domain", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(params);
}
function getParameterByName(name, url = window.location.href) {
name = name.replace(/[\[\]]/g, '\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
function validateEmail(email) {
// init
var mailformat = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
// check
if (email.match(mailformat)) {
return true;
} else {
return false;
}
}
<input type="text" id="reg_email" >
Especially for Dean:
<input type="text" id="reg_email" >
<script src="https://plungjan.name/SO/formval.js"></script>