By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,219 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,219 IT Pros & Developers. It's quick & easy.

Include Scripts

P: n/a
Hello,

I am working on a web site where I have a master page.
Most web site pages use this master page as base.

Some scripts are used in all pages ... other just in a few pages.
Should I load all scripts in master page ... I think they are cached
right?

Or should I load the common scripts to all pages in my master page and
then in each page load the scripts that are not used in all pages?

Thanks,
Miguel
Aug 17 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Aug 18, 8:43*am, shapper <mdmo...@gmail.comwrote:
Hello,

I am working on a web site where I have a master page.
Most web site pages use this master page as base.

Some scripts are used in all pages ... other just in a few pages.
Should I load all scripts in master page ... I think they are cached
right?
That depends on the size of the scripts and how often they change. It
is faster to put all script into one or two files as it cuts down on
the number of requests. If parts of the script change frequently, you
might want to put those parts in a separate file (if appropriate) to
optimise caching.

Or should I load the common scripts to all pages in my master page and
then in each page load the scripts that are not used in all pages?
If the extra script is small (say less than 8kB), put in the master
script file. If it's big, only load it when needed.

Even very large script files reduce to 20~30kB when minified and
compressed so size isn't really that much of a problem.
--
Rob
Aug 17 '08 #2

P: n/a
On Aug 18, 12:45*am, RobG <rg...@iinet.net.auwrote:
On Aug 18, 8:43*am, shapper <mdmo...@gmail.comwrote:
Hello,
I am working on a web site where I have a master page.
Most web site pages use this master page as base.
Some scripts are used in all pages ... other just in a few pages.
Should I load all scripts in master page ... I think they are cached
right?

That depends on the size of the scripts and how often they change. *It
is faster to put all script into one or two files as it cuts down on
the number of requests. *If parts of the script change frequently, you
might want to put those parts in a separate file (if appropriate) to
optimise caching.
Or should I load the common scripts to all pages in my master page and
then in each page load the scripts that are not used in all pages?

If the extra script is small (say less than 8kB), put in the master
script file. *If it's big, only load it when needed.

Even very large script files reduce to 20~30kB when minified and
compressed so size isn't really that much of a problem.

--
Rob
Is there any tool that merges various Javascript files into one single
file and minifies it?

I suppose that would be the best solution. Then I would use that file
on production mode ...

Thanks,
Miguel
Aug 18 '08 #3

P: n/a
shapper meinte:
Is there any tool that merges various Javascript files into one single
file and minifies it?
JSMin. There are implementations in various languages available. It only
removes comments and blanks and doesn't obfuscate. Combining several
scripts in to one has to be done manually though (my server side script
takes care of that).

Deliver JS files gzipped and you end up with miniscule file sizes. No
need for those idiotic "p,a,c,k,e,r"-scripts.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur fr den alpinen Raum
Aug 18 '08 #4

P: n/a
Or should I load the common scripts to all pages in my master page and
then in each page load the scripts that are not used in all pages?
I prefer that approach. Let each page be responsible for the burdens
it places on resources, both server and bandwidth. Any script
previously loaded is, as you suggested cached. Don't make the request
for an item unless it's required. Not everyone may require everything,
i.e., not all visitors will hit all pages - why make them and your
server pay the expense of unnecessary transfers?

Of course, this depends on whether you are undertaking some radical
minimization approach, such as composing all scripts into a single
file and then minify-ing before service. There is virtue to a smaller
number of separate files, but I'm a little afraid of sites that serve
such bloated masses of content that they need to worry about the tiny
extra volume of bytes that get pushed out in JS files...one fewer
images might be the better option.

In general, I stick to the preference for more files and less
confusion in the support arena. I don't do radical minification with
obfuscation. The prod code needs to look as much like the dev/test
code as possible. My minify-ing only strips non-line-break whitespace
and comments.

Aug 18 '08 #5

P: n/a
In comp.lang.javascript message <61b25f07-ab3a-462b-83df-f33a2c6dd695@34
g2000hsh.googlegroups.com>, Sun, 17 Aug 2008 17:49:08, shapper
<md*****@gmail.composted:
>
Is there any tool that merges various Javascript files into one single
file and minifies it?

I suppose that would be the best solution. Then I would use that file
on production mode ...
Much depends on the sort of people who are tempted to look at and into
your pages. But if your scripts have essential comment and a
reasonably-spaced layout you are more likely to get constructive
feedback on any errors and inefficiencies that they may show. And many
pages nowadays are so bloated with graphics that a small improvement in
script size, though in principle good, will not be significant.
For example, I noticed that JF (*.com) was using new Date("... CST") or
similar for an event occurring non-locally (in fact, over 10^9 km away
IIRC), predicted that Opera would get that wrong, verified the
prediction, mailed him, and it was fixed within an hour or two. But
another site (*.gov), similarly warned about mixing getYear and
getUTCMonth, has not yet fixed it. My js-date2.htm refers. Of course,
they *may* fix it before it next matters, at the Dec/Jan overlap. And
those with UK-like settings will not see any effect.

If your pages are coded as badly as many, you may reasonably want to
hide the script. You cannot make it executable but impossible to be
seen; but you can make it less easy to read so that casual observers may
not bother.

--
(c) John Stockton, nr London UK. replyYYWW merlyn demon co uk Turnpike 6.05.
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html-Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm: about usage of News.
No Encoding. Quotes precede replies. Snip well. Write clearly. Mail no News.
Aug 18 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.