Reputation: 1691
I'm including a script "foo.js" that I want to modify:
<head>
<script src="/myPath/foo.js" type="text/javascript"> </script>
</head>
To be able to hook into an ajax call on my main page:
$.ajax({
url: urlPath,
success: function (data) {
//hook into here from foo.js somehow??
}
});
Is there any way to do this solely in foo.js without modifying the main page's script at all? (perhaps hook into a call inside the main script that is filling out/changing content?)
Thanks!
Upvotes: 1
Views: 3131
Reputation: 5205
If your foo.js
is wrapped in a closure (if you don't know what this means, just ignore) then you can bind to a namespace and call functions through there.
Otherwise, javascript has a global scope so any scripts imported into your file will have the ability to call functions as long as it is imported in order of dependencies.
ie:
foo.js:
function foo(data) {
// do stuff with data
}
main.html:
<head>
<script src="/myPath/foo.js" type="text/javascript"></script>
<script type="text/javascript">
$.ajax({
url: urlPath,
success: function(data) {
foo(data);
}
});
</script>
</head>
It's important that the embedded script comes AFTER the import so that it can make reference to functions inside foo.js
.
Upvotes: 0
Reputation: 1420
You can try to use a Global Ajax Event Handlers for listning global events.
Upvotes: 5