Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Global function is undefinded in scope of html loaded using jquery .load()

I have html with :

<script src="/js/app.js"></script>

in app.js I have function foo:

function foo() { console.log("foo") }

Then I have jquery and use it's method .load to load external html content:

$("#modal-wrapper").load("/modal_dialog_ajax", function () {
   // Some logic
});

Output of /modal_dialog_ajax looks like:

<div>Some markup</div>
<script type="text/javascript">
 foo();
</script>

Javascript is executed fine but call of foo() is undefined in the scope of this html.

Why Js inside ajax loaded html content does not see global functions defined on the page?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Workarounded this using window global object:

function foo() { console.log("foo") }
window.foo = foo;

And then call it as

window.foo();

But not sure it is a good practice.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!