Issue
I have multiple duplicated external scripts that I want to run just once.
I would like to return early on those scripts after the first time doing a small modification to those scripts, something like:
if (window.this_script_have_been_loaded) {
something_like_return
}
// rest of script
window.this_script_have_been_loaded = true
But I do not want to add a big if
or function
around the existing code to make it easier to manage. Is there a return
statement for scripts?
Solution
To prevent code from running, place this code inside a block statement and exit this block if needed with a break
statement and a label.
label: {
console.log('before');
if (true) break label;
console.log('after');
}
Answered By – Nina Scholz
Answer Checked By – Marie Seifert (BugsFixing Admin)