You must Sign In to post a response.
  • Category: .NET

    Function declared as variable "function is not defined" error in js

    Hi,
    I got a sudden error in my web project. When i published it i am getting error on my js functions "Uncaught ReferenceError: ltrim is not defined"

    In my js file:
    ltrim = function(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    }

    but when i changed it to as below and its working:
    function ltrim (str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    }

    Please help me how to resolve and why this issue has came.

    Thanks
    Anil
  • #767315
    Hi,
    When you set the wrapping to any jQuery event, the functions you defined inside that block cannot be accessed outside that event and in your case you are using a separate JS file. so replace ltrim = function(str, chars) with:

    window.ltrim = function(str, chars)

    check this jQuery fiddle:
    http://jsfiddle.net/vSHQD/1/


Sign In to post your comments