I have the following:
$(function () {
$.ajaxSetup({ cache: false });
var dialogs = {};
var formSubmitHandler = function (e) {
...
}
}
then in another script I try to call
function dialogClick(link) {
$.get(viewUrl + parameters)
.success(function (content) {
if (content.match(/^[eE]rror/)) {
mvcOnFailure(data)
} else {
$.modal({
title: title,
closeButton: true,
content: content,
width: false,
resizeOnLoad: true
}).find('form').submit(formSubmitHandler).end();
}
})
Note that I have cut out parts of the script to make it easy to read. There are no script errors showing just the following error:
In the second script I get an error message saying "SCRIPT5009: 'formSubmitHandler' is undefined' in Internet Explorer.
Am I calling it wrongly? I thought the function would be global and when I check the script that it is inside of is attached to the page.
4 Answers 4
No, it's not global; your "formSubmitHandler" function is declared within the "ready" callback in the first block of sample code you posted. It's therefore private to that function.
What you could do, if you really want a global function, is:
window['formSubmitHandler'] = formSubmitHandler;
in the first function. Or, alternatively, you could make it a jQuery "global" function:
$['formSubmitHandler'] = formSubmitHandler;
In that case, you'd get to it as $.formSubmitHandler.
Comments
Try moving your function out of the function block e.g
$(function () {
$.ajaxSetup({ cache: false });
var dialogs = {};
}
var formSubmitHandler = function (e) {
...
}
Comments
formSubmitHandler only exists within the function scope you declare it, since you used the var variable.
You need to either:
- declare
dialogClickin the same scope - declare
formSubmitHandlerin the global scope, usingwindow.formSubmitHandleror simplyfunction formSubmitHandler(){}
Comments
formSubmitHandler is a function declared in a scope not visible for the dialogClick() function
So
- Either you declare
formSubmitHandlerasglobal - or you define the function
dialogClickinsidedocument.readyfunction (andformSubmitHandleris reachable since is in a parent scope)