0

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?

Community
  • 1
  • 1
Melanie
  • 3,021
  • 6
  • 38
  • 56
  • 1
    if you are using jQuery, did you try $(document).ready(function() { /* your code */ });, and should $find be $.find, or just a selector – spojam Dec 30 '13 at 17:41
  • Is anything going on in your browser's error console? – Michael Berkowski Dec 30 '13 at 17:42
  • What are `$find()` and `get_textBoxControl()` methods? – A. Wolff Dec 30 '13 at 17:44
  • 1
    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? – robertc Dec 30 '13 at 18:07
  • robertc - please put your comment on the lack of a close bracket in the Answer section and I'll mark it correct. That was the problem. I knew it was something like that that I just wasn't seeing. Thank you! – Melanie Dec 30 '13 at 19:40
  • As to why I'm not using $(document).ready, my jquery is just babysteps - I'm only using a few lines that I found on the web that solved a problem for me with my comboboxes. If someone could explain exactly how to use $(document).ready, I'd love to try it, but I tried spojam's suggestion within my script tags and it didn't work. As to A. Wolff's questions, $find() is a jquery function and get_textBoxControl() method is a method I've found that works well with comboboxes. It may be something that only works with AJAX controls; I don't remember at this point. – Melanie Dec 30 '13 at 19:43
  • `$find()` is not a jQuery function. – robertc Dec 31 '13 at 13:56

1 Answers1

1

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);
robertc
  • 74,533
  • 18
  • 193
  • 177