0

If the following possible? I wish to move the alert(result) into a function and to dynamically call it.

Current

$.ajax(this.href, {
    success: function (result)
    {
        alert(result);

        AjaxComplete();
    }
});

My Attempt - not working

$.ajax(this.href, {
    success: function (result)
    {
        window["MyAlert(result)"]();

        AjaxComplete();
    }
});

function MyAlert(result)
{
    alert(result);
}

Is this possible?

2 Answers 2

3

Why can't you just do this?

MyAlert(result);

If MyAlert is a part of the window object, it's already a global.

Unless you want to call an arbitrary function by name (which isn't really good practice, IMO), which you can do like this:

window[function_name_string](argument);
Sign up to request clarification or add additional context in comments.

1 Comment

because I want to store the function in an attribute. data-ajax-onsuccess-withparam
1
window["MyAlert(result)");

is invalid syntax (missmatching [ and ), wrong function name, and not calling it at all, just getting it..). Should be

window["MyAlert"](result);

if you want to call it like that, but I see no reason why you couldn't just call it normally, as Blender mentioned.

1 Comment

ahh yes, the ]) thing was a typo from typing the question. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.