mardi 16 août 2016

Call function from another js file when using iife pattern?

Is it possible to call function that is in one js file and it is made public by usage of iife pattern, in another js file? For example, this is some validationScript.js file:

var ValidationNamespace = function () 
{
    var nameValidation = "";
    var testValidation = "";

    return{
        validateIfNameAlreadyExistsInTable : function(currentNameValues)
        {
            for(var i=0; i < currentNameValues.length ; i++)
            {
                if(document.getElementById("tbName").value == currentNameValues[i])
                {
                    var nameVal = "Name already exists in table!";
                    document.getElementById("nameVal").innerHTML = nameVal;
                    return false;
                }
            }
            document.getElementById("nameVal").innerHTML = "";
            return true;
        }
   };
}();

And I have another file, lets say script.js

Is there some way to call function validateIfNameAlreadyExistsInTable from some script.js function?

The problem is that it is undefined when I try to call it:

enter image description here

I have index.html where I am referencing both .js files like this:

<script type="text/javascript" src="script.js"></script>
<script type="text/javascript" src="validationScript.js"></script>

Aucun commentaire:

Enregistrer un commentaire