Pros/Cons to loading Javascript inside the BODY tags?

  • Thread starter Thread starter darrel
  • Start date Start date
D

darrel

I have a need to load javascript functions for various parts of our site.

Normally, when making a page, we link to the javscript function within the
HEAD tags and reference them as needed within the contents of the page.

However, all of our content is being loaded by a user control already within
the BODY of our page.

What's the best way to handle this?

Should the UC that loads within the body tag somehow communicate with the UC
that loads the links in the HEAD tags to let it know that we need a
particular .js link? If so, what's the best way to pass that info to that
UC? I could just have that UC query the DB as well, but 99% of the time,
that'd be a pointless thing, as usually we wouldn't need a .js link up there
(of course, this would be an easy solution to implement).

Alternatively, any reason to not just load the .js that is needed within the
BODY of the page itself?

-Darrel
 
in your usercontrol you should be registering the script you need:


if (!cm.IsStartupScriptRegistered("keyofscript"))
{
cm.RegisterClientScriptBlock(this.Page.GetType(),
"keyofscript","pathtojsfile");
}


the check is to make sure that if you drag multiple controls (or even
other controls dependent on the same js file), it will only be loaded
once.


i've found very few situations where the js needs to actually be in the
<head>, and even the framework doesn't do that for you. there are some
situations where i just needs to be there, and in those rare cases, i
hardcode the js link in the master page <head> section
 
Back
Top