So I’ve got a unique problem.
I’ve got a site that I can’t edit the template for at all, I can only alter the embedded script.
This embedded script is injected into the footer of the site in question.
The embedded script relies on $(document).ready
to kick itself off.
The problem is, a script higher up on the page throws an error in it’s $(document).ready
function which prevents my $(document).ready
from ever being called.
I tried setting up a try .. catch
block, like this, but I believe this will only work if my script is higher up on the page still.
My question, is it possible to get my $(document).ready
call to run, even if it’s lower on the page, and a previous one has errors?
In the site above my script:
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#logo').vAlign();
});
</script>
.vAlign();
is not defined so it’s throwing: Uncaught TypeError: undefined is not a function
In my embedded js:
jQuery(document).ready(function() {
console.log('ready!');
});
I never see “ready!” in the console.
When important operations must always be executed finally-codeblock of try-catch could be used. Even if catch-codeblock is not run through finally-codeblock is and so callReady-function is called definitely at the end no matter whether there was an error or not(except for syntax errors). As it is the case below:
Unfortunately if there is no error callReady is called twice(because finally is always run through at the end) but you can check for this case:
An error in one script tag in the page doesn’t keep other scripts from running.
Example:
This will first log a syntax error for the second script tag, then it will log the values 1 and 3 as the other ready events work fine.
Demo: http://jsfiddle.net/Guffa/am4f7f18/