简体   繁体   中英

Javascript indexOf condition not behaving as expected

I have a javascript function that runs on window.onload:

if(window.onload) {
        var curronload = window.onload;
        var newonload = function() {
            curronload();
            formatICCID_IMEI();
        };
        window.onload = newonload;
    } else {
        window.onload = formatICCID_IMEI;

function formatICCID_IMEI() {
        var IMEI = $find("<%=cbIMEI.ClientID %>");
        alert(IMEI.get_textBoxControl().value);
        alert(IMEI.get_textBoxControl().value.indexOf("."));
        if (IMEI.get_textBoxControl().value.indexOf(".") > -1) {
            alert("Hi!");                
        }

    }

I'm using this more elaborate way of calling my function from this link because if I just use window.onload or document.onload, my control (cbIMEI) is not found. Using this more elaborate method, I don't have that problem. However, my function formatICCID_IMEI is acting strangely. I don't know if it's due to the way I'm calling formatICCID_IMEI, or just something in formatICCID_IMEI that I'm not seeing. If I comment out

if (IMEI.get_textBoxControl().value.indexOf(".") > -1) {
            alert("Hi!");  

the first and second alerts tell me that

IMEI.get_textBoxControl().value = 351937.04.230880.7

and that

IMEI.get_textBoxControl().value.indexOf = 6

all as expected. HOWEVER, if I comment out the two above alert lines and uncomment the IF condition, the line

alert("Hi!");

never runs. If I uncomment all lines, none of the alerts run. The same behavior holds true if I'm in debug mode. If the condition is uncommented, my cursor never gets to my function at all. What the heck?

You have no close bracket for the if(window.onload) condition - is that intentional?

Since you're using jQuery, why are you not just using the standard $(document).ready stuff?

function formatICCID_IMEI() {
    var IMEI = $find("<%=cbIMEI.ClientID %>");
    alert(IMEI.get_textBoxControl().value);
    alert(IMEI.get_textBoxControl().value.indexOf("."));
    if (IMEI.get_textBoxControl().value.indexOf(".") > -1) {
        alert("Hi!");                
    }

}

$(document).ready(formatICCID_IMEI);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM