I’m trying to work with different loaded JavaScript in my page, but I’m not able to let them talk.
<html>
<script type="text/javascript" src="jquery144.js"></script>
<script type="text/javascript" src="shared_functions.js"></script>
<script type="text/javascript" src="page_function_callers.js"></script>
</html>
// shared_functions.js
$(document).ready (function () {
function sayHello (what) {
alert (what);
}
});
// page_function_callers.js
$(document).ready (function () {
$("div.my_button").click (function () {
sayHello ("Hello world!");
});
});
I work with jQuery and I’d like to use this way because it should let me recyle many public methods. Where I’m wrong?
When you declare “sayHello” (does anybody remember that Charlotte Church song? Gosh, the memories …) inside the “ready” function, it’s local to that function. You can make it global if you like by doing this:
(It’s nice to give the function a “local” name — the name after the “function” keyword — because then the function won’t show up as “anonymous” in Firebug.)
I would encourage you to investigate making your global function into a jQuery plugin, or at least a jQuery “global” by putting it on the jQuery object instead of “window”.