Search code examples
javascripthtmlself-invoking-function

Using <a href="javascript:myFunction()"> to access a function in an external js file


When I include myFunction() within the same HTML page of <a href="javascript:myFunction()"> the function is called fine upon click of the a tag.

But when myFunction() is located in an external JS file within a self invoking function, the function does not get called.

What format should the call be to access this function?

The external file looks like:

(function (window, document, $, undefined) {
    function myFunction() {

    }
})(window, document, jQuery);

Solution

  • The function you want to call is a local function declared inside a immediate function. You have no way to call it outside of it's scope.

    If you can modify your external file, you could export the function on window object like this:

    (function (window, document, $, undefined) {
        window.myFunction = function() {
    
        }
    })(window, document, jQuery);