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?

User97693321
3,3467 gold badges48 silver badges69 bronze badges
asked Sep 13, 2012 at 3:18

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);
answered Sep 13, 2012 at 3:19
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.

answered Sep 13, 2012 at 3:22

1 Comment

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.