禁用元素的Javascript代码仅适用于chrome

时间:2016-09-16 23:29:34

标签: javascript internet-explorer firefox cross-browser

我是javascript的新手。

我有当前的javascript代码,我已放置在我的网站的末尾,如果满足某个条件,则禁用按钮。但是,这个代码只在chrome上,当在firefox和Internet Explorer上测试时,似乎代码根本不起作用。

这是javascript代码:      

/** Email checker **/ 

console.log("Beginning of Code"); 
var reject = [
    "gmail.com",
    "yahoo.com",
    "msn.com",
    "rocketmail.com",
    "aol.com",
    "prodigy.com",
    "bellsouth.net", 
    "hotmail.com",
    "live.com",
    "icloud.com",
    "justenough.com",
    "blueridgeinventory.com",
    "jda.com"
    ]; 
var email = document.querySelector("input[name='Email']");
var submitButton = document.querySelector("input[type='submit']"); 
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\    [[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

function insertAfter(referenceNode, newNode) {

    if (document.getElementById("email-warning")){
        var element = document.getElementById("email-warning");
        element.parentNode.removeChild(element);
    } 
        referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}

var validateEmail = function() { 
    var input = email.value; 
    input = input.replace(/ /g,''); 

    var value = input.split('@'); 
    var isValidEmail = re.test(input); 
    var paragraphNode = document.createElement("p"); 
    paragraphNode.setAttribute("id","email-warning"); 

    if (isValidEmail) {
    /** Email is in rejected list **/ 
        var textNode = document.createTextNode("Your Email is Valid");              
        paragraphNode.setAttribute("style", "color: green; ");              
        submitButton.disabled = false; 
        submitButton.style.opacity = "1"; 

        if (reject.indexOf(value[1]) >= 0){
            var textNode = document.createTextNode("Please Enter Company Email");               
            paragraphNode.setAttribute("style", "color: red; ");   
            submitButton.disabled  = true; 
            submitButton.style.opacity = "0.5"; 
        }

    } else {
        var textNode = document.createTextNode("Your Email Invalid");               
        paragraphNode.setAttribute("style", "color: red; "); 
        submitButton.disabled  = true; 
        submitButton.style.opacity = "0.5"; 
    }

    paragraphNode.appendChild(textNode); 
    insertAfter(email, paragraphNode);

    console.log("This ran"); 

}

email.addEventListener('keyup', validateEmail); 
console.log("This ran"); 

/** Get the Previous page referral **/


var oldURL = document.referrer;  
var referringPageInput = document.getElementById("referringPage"); 

referringPageInput.setAttribute("value", oldURL ); 
console.log(oldURL);

函数EmailValidate中的代码似乎失败了。 我已经检查了控制台并且正在定义所有变量,但由于某种原因,该功能不适用于Internet Explorer和Firefox。我读到这些网络浏览器的样式计算机是不同的,但它从来没有给我一个替代品。任何帮助表示赞赏谢谢。

失败我的意思是它根本不起作用。控制台上没有错误或警告日志。我尝试添加一个console.log来查看代码是否在某个时间点失败,但一切似乎都运行正常。

0 个答案:

没有答案