0

When calling the below JavaScript method, the first alert is printed. Not even "Before return" alert is printed. Only if I do an F12, then the other alerts are shown. Please suggest!

function validateFPNotes() {
    var message = "";
    var notesId = document.getElementById('FANManagementForm:FANNotesTextIp');
    alert("validateFPNotes called and the notesid is :" + notesId);
    if (notesId != null) {
        alert("Not Null Block");
        var notesVal = trimString(notesId.value);
        alert("Notes Val:" + notesVal);
        window.console.log("validateFPNotes::notesId.value = " + notesId.value);
        alert("doc.getEletById(maxLengthNotes)" + document.getElementById(maxLengthNotes));
        var maxLength = document.getElementById(maxLengthNotes).value;
        alert("maxLength is:" + maxLength);
        if (notesVal != null && notesVal.length > maxLength) {
            message = message + replaceMaxLengthMessageTokens(document.getElementById(maxLengthExceedMessage).value, 'Notes', maxLength);
            alert("Message :" + message);
        } else {
            alert("Null Block");
            window.console.log("validateFPNotes::notesId.value = " + notesId.value);
        }
        if (message.length > 1) {
            alert(message);
            return false;
        }
    } else {
        alert("Null Block");
    }
    alert("Before return");
    return true;
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
maryJane
  • 117
  • 3
  • 16
  • 3
    It means that you're having an error in between `if`. Did you check browser console for errors? I think error should be in `trimString()` – Dhaval Marthak Feb 26 '14 at 15:28
  • then am not even getting this alert: alert("Not Null Block");. Browser Console? am not sure of it. – maryJane Feb 26 '14 at 15:35
  • To open console in Google Chrome: Right Click -> inspect element- in the end you will see `Console` tab click on it and you will see some errors in **red** Or Press `F12` as you know it :) – Dhaval Marthak Feb 26 '14 at 15:39

2 Answers2

0

Unless your code has encountered an error, in which case would prevent the rest of your code from running, you have a return false; if (message.length > 1). If this is hit, your alert("Before return") would never be executed.

ORION
  • 2,374
  • 2
  • 22
  • 31
  • Should this mean, if (notesId != null) has something to do with?, as am not getting the next immediate alert, until I do an F12 – maryJane Feb 26 '14 at 15:37
  • I didn't run your code, but it could be. You have notesId declared as an object, not a value of that element. Did you do this intentionally or did you mean to use document.getElementById('FANManagementForm:FANNotesTextIp').value;? – ORION Feb 26 '14 at 15:48
  • Did you write your own trimString() function? I don't see it declared anywhere. If it's not declared, that could be your error. – ORION Feb 26 '14 at 15:50
  • trimString() is there used. – maryJane Feb 26 '14 at 15:53
  • I see that it is being called, but that isn't a built in function. JavaScript uses .trim(). jQuery uses $.trim(). Also important to remember that the javascript .trim() isn't supported in IE8 and IE versions prior to IE8. – ORION Feb 26 '14 at 15:57
  • I can't see what is in your trimString() function because the declaration isn't in your code that is shown, as stated in the above comments, your error probably resides in that function. – ORION Feb 26 '14 at 15:59
  • trimString() I removed and still the same. – maryJane Feb 26 '14 at 16:13
0

Finally found the reason. Because windows.console is not available before F12-ing, the issue arose. I added this, and it worked fine, after adding this.

 if (window.console && window.console.log) {
                window.console.log("validateFanPermissionsNotes::notesId.value = "
                        + notesId.value);
            }

A simila instance here: I.E. craches my JS script at first, then i press F12 and it works beaultifully

Community
  • 1
  • 1
maryJane
  • 117
  • 3
  • 16