I have a function defined inside a $('document').ready.
$('document').ready(function() {
function visit(url) {
$.ajax({
url: url,
container: '#maincontainer',
success: function(data) {
init();
}
});
}
function init() {
...
}
)};
But when I call init() in Chrome Console I get : ReferenceError: init is not defined.
Update: Thank you all for your help. I didwindow.init = init; and it works perfectly.
Your
initfunction is contained by the scope of the function you’ve passed tojQuery.ready. This is a good thing, it means you haven’t created an unnecessary global.If you need to export the function to the global scope, you can do so by explicitly assigning to a property on
window, e.g.:Since
windowis the global object on browsers, that would allow you to call it from Chrome’s console without thewindow.prefix. But only do that if absolutely necessary, the global scope is already crowded enough.