I am including an external JS file, which has some code that I need to run after a particular thing happens (after contact form is submitted).
The external file has a function but also calls the function. This isn't much of a pain if it was OK to run that function on page load, but I need to run its function after a form is submitted. My form is submitted via AJAX, so I need to run the external file's function after the form has submitted, only.
How can I do this?
Here's an illustration to help...
MY SITE'S FOOTER:
<script type="text/javascript">
var a_variable = 12345;
</script>
<script src="//domain.com/external.js" type="text/javascript"></script>
</body>
</html>
CONTENTS OF EXTERNAL.JS:
function doThis(){
alert(window.a_variable);
}
doThis();
But I only want to run doThis()
at a time that I want, not on page load.
Can I stop doThis();
from executing until I explicitly tell it to?
NOTE: I have already tried including external.js
by creating a script tag (in javascript) and loading it that way, but the function in external.js
needs to write to my users' browser, to which the users' browser says Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
. After that, I looked into the solutions in this answer, but it did not solve the problem. So, I am looking for a way to stop doThis()
from executing.