Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Prevent loaded Javascript file from executing after running another function

So I have 3 files:

web.html
java1.js
java2.js

I’m loading java1.js in the html. When this runs it writes into web.html with a list of items.

<script type="text/javascript" src="../java1.js"></script>

Then I have jQuery that executes a function in java2.js. What this is supposed to do is populate the html with a different list of items. This code does work because if I load it instead of java1.js, I get a different list.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<a href="#" id ="web" onclick="javascript: $.getScript('java2.js' , function (){ web() ; } );" />

Where I’m having a tough time is, even though java2.js is executing successfully, it doesn’t refresh the data in the html. That seems to make sense so I added this to java2.js

window.location = window.location

and now it does refresh the html but, it reloads java1.js since it loads when the html loads and I’m back to having the original list.

So finally my question is, what’s the best way to execute java2.js without loading java1.js data back into the html?
Thanks you much!

>Solution :

You can store data in localStorage once the data has been loaded, and then check for that data in localStorage before updating the lists:

Java 1:

if (!localStorage.getItem("dataLoaded")) {
  lists = []; // set the initial values
}

Java 2:

lists = []; // set new values
localStorage.setItem("dataLoaded", true);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading