Pros/Cons to loading Javascript inside the BODY tags?

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
 
K

kferron

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,348
Latest member
RoscoeNevi

Latest Threads

Top