Hi Kalle,
I’d like to add more clarification 
The scope depends on how and where it’s declared, so we can’t really say it doesn’t have or have a global scope. It depends on the user who is adding the code. But the term global that is related the theme only means add to ALL
pages in contrast to cornerstone CSS which is LOCAL
to specific page. It’s completely unrelated to javascript scope which completely depends on how user coded it.
From previous reply, I only provided one possible solution but there are others too. Example,
As this works as my previous recommendation,
jQuery( function( $ ) { /* code block 1 */
function testfn(){
alert("test");
}
$(document).ready(function(){
$("body").on("click","#testbtn",function(){
testfn();
});
});
} );
This also works,
function testfn(){
jQuery( function( $ ) { /* code block 1 */
alert("test");
});
}
jQuery(function($){ /* code block 2 */
$(document).ready(function(){
$("body").on("click","#testbtn",function(){
testfn();
});
});
} );
That’s because testfn()
became global (not Theme’s term global
, but javascript scope). How do we know which is global and local? This code should explain it
<script>
/* anything place here is global, variable or function */
var global_width = 100;
function lets_multiply () { /* this is the same as jQuery ( function() {} ); */
/* anything here is local to lets_multiply () */
var local_width = 100;
alert ( global_width * local_width );
function say_hello() {
alert('HELLO');
}
}
/* anything place here is global, variable or function */
function lets_divide() { /* this is the same as jQuery ( function() {} ); */
alert ( global_width / local_width );
say_hello();
}
/* anything place here is global, variable or function */
</script>
If you call lets_multiply()
, it works without an issue. But try calling lets_divide()
, it will have error since local_width
is not defined globally, nor say_hello()
. They belong to lets_mulitply()
, and even if you call them anywhere on other pages, it will not work since they are locally declared within existing function.
Again, all of this is not related Theme’s global javascript placement, it’s just a term for making the added script available for all pages, it doesn’t have capability to compile the script to decide which is global, it’s still the browser that executes it.
Thanks!