lundi 20 janvier 2020

Call Function from if statement

I have a button on my UI which when clicked checks the URL of the current tab compared to one which is passed in from database. If they match I want to call a function which makes an ajax call to get some data. My code is as follows:

('#automate').click(automateButton);


                       function automateButton() {
                            if(webpageUrl == activeTabUrl){
                                //call function here
                            }
                            else {
                                // Window opens
                                window.open(webpageUrl);
                            }
                        }

This is my ajax call:

$('#scenarioDropdownList').change(function () {
    var scenarioId = $('#scenarioDropdownList option:selected').attr('id');
    getData(scenarioId); 
    });

    function getData(scenarioId) {
        $.ajax({
            type: "GET",
            url: 'http://localhost:54442/api/scenariodatas/GetScenarioData',
            data: {scenarioId: scenarioId},
            dataType: 'JSON',
            success: scenarioData,
            error: function(){
                    console.log("There has been an error retrieving the data");
            }
        });
    }

    function scenarioData(response) {
        $.each(response, function(key, val) {
                var fieldType = val.fieldType;
                var fieldName = val.fieldName;
                var fieldValue = val.fieldValue;

                var field = $(fieldName);
            })
        }

I want to be able to call the ajax call from inside of the if statement. How would i go about this?

Aucun commentaire:

Enregistrer un commentaire