Search code examples
javascriptreferenceerror

ReferenceError: function not defined


I am trying to invoke a javascript method. I am building the html at run time by string concatenation.

$scope.getRoute = function (isRouteFormValid) {
    routingDemoPageService.executeService(serviceURL, 'admin', 'admin').then(function (response) {
        function tryingOnceAgain() {
            alert('called.....');
        }

        var markers = L.markerClusterGroup({
            showCoverageOnHover:false,
            chunkedLoading: true
        });

        var geojsonLayer = L.geoJson(response, {
            onEachFeature: function(feature, layer){

            var UIDValue = (feature.properties['uid'] !== null ? Autolinker.link(String(feature.properties['uid'])) : '');

            var popupContent = '<table>' +
                            '<tr><th scope="row"><a href="javascript:tryingOnceAgain()">Edit</a></th><td></td></tr>' +

                            '<tr><th scope="row">uid</th><td>' + UIDValue + '</td></tr></table>';

             layer.bindPopup(popupContent);
         }
     });
     markers.addLayer(geojsonLayer);
     $scope.map.addLayer(markers);
     $scope.map.fitBounds(markers.getBounds());

   })['catch'](function (error) {

 });

}

When i click on the link, which invokes tryingOnceAgain method, i am getting following error

ReferenceError: tryingOnceAgain is not defined

I am not sure why i am getting following error.

Can someone please provide any pointers what am i doing wrong.


Solution

  • javascript:tryingOnceAgain() is referenced to a function in the global scope but you defined tryingOnceAgain function inside function (response) { scope.

    To fix that you have to move your tryingOnceAgain function to global scope.

    Or just assign it to window object without changing physical place:

    window.tryingOnceAgain = function() {...}