I am absolutely NOT a JavaScript guru and I make no warranty that what I'm about to say is 100% correct. So here I go....
It raises another question. Since the contents of p_web.script() are wrapped in a document.ready function and placed at the bottom of the page HTML, does this mean it is "global"? That is, does it "run" all the time? Or does it need to be called when needed? I'm not sure I asked that correctly, hopefully you get my drift!
The code in p_web.Script will only exist if the procedure it's used in is open. So if you used p_web.Script in JeffsCoolForm the that code will only exist at the bottom of your html when JeffsCoolForm is open. Therefore, it is in scope at that time.
That brings up scope.
So p_web.Script places your custom code inside a function like this:
$(document).ready( function(){
function jeffsFunction() {};
});
At that point jeffsFunction has a scope of the inside of the $(document).ready( function() .
Placing jeffsFunction there give the page/document time to get "ready" to ensure all of the page's elements are ready to be accessed by the Javascript.
You'll also run across this sometimes: $(window).load( function(){
Notice one is (document) and the other is (window).
The differences are:
ready() fires when the DOM (HTML) is ready and scripts are loaded.
load() fires when everything else has finished loading too: HTML, Scripts, CSS, Images
Anyway, back to scope...
Global Variablevar imGlobal = "global";
function jeffsFunction() {
};
or a non-declared variable with an assigned value (I'm 80% sure I'm right on this one):
function jeffsFunction() {
imGlobal = "global";
};
Local Variablefunction jeffsFunction() {
var imLocal = "local";
};
I do this a lot in my custom.js file:
$("#JeffsCoolForm").ready( function(){
function jeffsFunction() {
//Run some cool stuff here
};
});
Now, when JeffsCoolForm is "ready", I can execute some custom JS "on the fly" so to speak.
Also, since it is placed at the end of the page HTML, I guess the embed you use to set p_web.script() makes no difference. Is this correct?
I won't say that it makes no difference but I generally, no.