-1

After i load this :

$('#buy-div').load('../buyBar/buyBar.html',function() {
 //do some things here
 });

I would like to include this :

<script src="../buyBar/BuyBar.js"></script> //access some html that does not exist

Beacuse in this js i am looking for some html that does not exist only after the .load function is done. (such as getElementById or $('input').keyup(function() { that happens before the .load was finished.

2
  • This may help! Commented Jun 27, 2019 at 7:15
  • Just because the elements don’t exist yet, does not mean you have to load the script itself only after they do … Wrap that part of the functionality into a function, that you can then call in the load method callback. Commented Jun 27, 2019 at 7:29

2 Answers 2

1

Just put the code that you want to run after the html is loaded in a function. Then call that function in the callback function of the .load('../buyBar/buyBar.html')

Assume "../buyBar/BuyBar.js" originally contains

document.getElementByID("#someElement").innerHTML = "...";

You can change it to

function someFunction(){document.getElementByID("#someElement").innerHTML = "...";}

Now just put <script src="../buyBar/BuyBar.js"></script> in the <head> as usual. Then do this:

$('#buy-div').load('../buyBar/buyBar.html',function() {
    someFunction();
    //do other stuff   
});
Sign up to request clarification or add additional context in comments.

Comments

-1

I figure out i can do it by loading it when .load is done :

     let script = document.createElement('script');
      script.src = "../buyBar/Pay.js";
      document.body.append(script);  

This works but i am not sure is the best solution.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.