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

Do <script tags have to appear in <head section?

P: n/a
Hi

I am new to javascript. I have a website running in .net, that has a
load of these in the <head section of the default.aspx page, which
bumps the page size up to 371kb. However, many of these are only used
in one 'skin', or page in the system. Is there any way I can put these
scripts elsewhere, so they are only loaded when they are needed. Then
every webpage will not have to be so enormous?

This is the sort of thing I am talking about:

<script type="text/javascript" src="<%= Page.ResolveUrl("js/
swfobject.js") %>"></script>

Stapes

Oct 11 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Stapes vient de nous annoncer :
Hi

I am new to javascript. I have a website running in .net, that has a
load of these in the <head section of the default.aspx page, which
bumps the page size up to 371kb. However, many of these are only used
in one 'skin', or page in the system. Is there any way I can put these
scripts elsewhere, so they are only loaded when they are needed. Then
every webpage will not have to be so enormous?

This is the sort of thing I am talking about:

<script type="text/javascript" src="<%= Page.ResolveUrl("js/
swfobject.js") %>"></script>

Stapes
using this method, you just download the .js file once, and every time
your code uses it, the browser looks for it in the client cache no?

take a look at this

http://www.fiddlertool.com/fiddler/

its a html debugger

you can get information about all the traffic and times of your pages,
forms and a lot of things
Oct 11 '07 #2

P: n/a
On 11 Oct, 10:40, matatunos <matatu...@gmail.comwrote:
Stapes vient de nous annoncer :
Hi
I am new to javascript. I have a website running in .net, that has a
load of these in the <head section of the default.aspx page, which
bumps the page size up to 371kb. However, many of these are only used
in one 'skin', or page in the system. Is there any way I can put these
scripts elsewhere, so they are only loaded when they are needed. Then
every webpage will not have to be so enormous?
This is the sort of thing I am talking about:
<script type="text/javascript" src="<%= Page.ResolveUrl("js/
swfobject.js") %>"></script>
Stapes

using this method, you just download the .js file once, and every time
your code uses it, the browser looks for it in the client cache no?

take a look at this

http://www.fiddlertool.com/fiddler/

its a html debugger

you can get information about all the traffic and times of your pages,
forms and a lot of things
Surely an http debugger?

Oct 11 '07 #3

P: n/a
On 11 Oct, 10:40, matatunos <matatu...@gmail.comwrote:
Stapes vient de nous annoncer :
Hi
I am new to javascript. I have a website running in .net, that has a
load of these in the <head section of the default.aspx page, which
bumps the page size up to 371kb. However, many of these are only used
in one 'skin', or page in the system. Is there any way I can put these
scripts elsewhere, so they are only loaded when they are needed. Then
every webpage will not have to be so enormous?
This is the sort of thing I am talking about:
<script type="text/javascript" src="<%= Page.ResolveUrl("js/
swfobject.js") %>"></script>
Stapes

using this method, you just download the .js file once, and every time
your code uses it, the browser looks for it in the client cache no?

take a look at this

http://www.fiddlertool.com/fiddler/

its a html debugger

you can get information about all the traffic and times of your pages,
forms and a lot of things
Sorry - I don't see the relevance of your answer

Oct 11 '07 #4

P: n/a
>>
>using this method, you just download the .js file once, and every time
your code uses it, the browser looks for it in the client cache no?

take a look at this

http://www.fiddlertool.com/fiddler/

its a html debugger

you can get information about all the traffic and times of your pages,
forms and a lot of things

Sorry - I don't see the relevance of your answer
sorry, i have not read correctly the original post, my native language
is spanish not english.

answering to Captain Paralytic

i'm a beguinner in web development, i use firebug with mozilla to get
information about web pages, scripts, gets, posts, xml returned by ajax
etc, with IE6 and IE7, i didnīt find any thing like firebug, and
fiddlertool is a very good tool for me to see why some things are wrong
in my code, performance, headers, broken links, traffic, etc.. its not
a debugger, but helps debugging (the autor say "Web Debugging Proxy")
Oct 11 '07 #5

P: n/a
On 11 Oct, 12:33, RobG <rg...@iinet.net.auwrote:
How does the above address the OP's issue of very large pages?
Well, the following 2 extracts from the referenced page explain this:

The problem
I was wondering how feasible it is to call Javascripts only when you
need them. Basically not to have a load of <script src="js.js">s at
the top of the page, but only those that are actually needed, when
they are needed.
Javascript include on user demand
Now a bit more of a real life example - the javascript is included
when the user performs an action (click).

Oct 11 '07 #6

P: n/a
Stapes <st**********@gmail.comwrote:
>Hi

I am new to javascript. I have a website running in .net, that has a
load of these in the <head section of the default.aspx page, which
bumps the page size up to 371kb. However, many of these are only used
in one 'skin', or page in the system. Is there any way I can put these
scripts elsewhere, so they are only loaded when they are needed. Then
every webpage will not have to be so enormous?

This is the sort of thing I am talking about:

<script type="text/javascript" src="<%= Page.ResolveUrl("js/
swfobject.js") %>"></script>
You're talking about using server-side processing to create a filename
for the src attribute of the script tag? That looks OK to me. By the
time the client gets the page, src has a normal-looking filename in
it, and the client browser will just go and get that file.

--
Tim Slattery
Sl********@bls.gov
http://members.cox.net/slatteryt
Oct 11 '07 #7

P: n/a
On 11 Oct, 14:14, Tim Slattery <Slatter...@bls.govwrote:
Stapes <steve.sta...@gmail.comwrote:
Hi
I am new to javascript. I have a website running in .net, that has a
load of these in the <head section of the default.aspx page, which
bumps the page size up to 371kb. However, many of these are only used
in one 'skin', or page in the system. Is there any way I can put these
scripts elsewhere, so they are only loaded when they are needed. Then
every webpage will not have to be so enormous?
This is the sort of thing I am talking about:
<script type="text/javascript" src="<%= Page.ResolveUrl("js/
swfobject.js") %>"></script>

You're talking about using server-side processing to create a filename
for the src attribute of the script tag? That looks OK to me. By the
time the client gets the page, src has a normal-looking filename in
it, and the client browser will just go and get that file.

--
Tim Slattery
Slatter...@bls.govhttp://members.cox.net/slatteryt
How is that relevant to this discussion?

Oct 11 '07 #8

P: n/a
On 11 Oct, 16:11, Stapes <steve.sta...@gmail.comwrote:
Doesn't work.
"Doesn't work." How helpful. Care to share how it doesn't work? What
actually happens? What are you expecting to happen?

If your function OpenTour() is in the library js/virtualtour.js, then
why not do away with the href="javascript... and just do something
like:

<li><a class="pdflink" href="#" onclick="include_dom('http://www.hotel-
victoria.co.uk/js/
virtualtour.js');OpenTour('beach','beach_medres.ip x');return
false;">Beach</a><br /></li>

Oct 11 '07 #9

P: n/a
On 11 Oct, 12:00, matatunos <matatu...@gmail.comwrote:
using this method, you just download the .js file once, and every time
your code uses it, the browser looks for it in the client cache no?
take a look at this
>http://www.fiddlertool.com/fiddler/
its a html debugger
you can get information about all the traffic and times of your pages,
forms and a lot of things
Sorry - I don't see the relevance of your answer

sorry, i have not read correctly the original post, my native language
is spanish not english.

answering to Captain Paralytic

i'm a beguinner in web development, i use firebug with mozilla to get
information about web pages, scripts, gets, posts, xml returned by ajax
etc, with IE6 and IE7, i didnīt find any thing like firebug, and
fiddlertool is a very good tool for me to see why some things are wrong
in my code, performance, headers, broken links, traffic, etc.. its not
a debugger, but helps debugging (the autor say "Web Debugging Proxy")
And headers, traffic, &c. are not HTML

Oct 11 '07 #10

P: n/a
On 11 Oct, 16:27, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 11 Oct, 16:11, Stapes <steve.sta...@gmail.comwrote:
Doesn't work.

"Doesn't work." How helpful. Care to share how it doesn't work? What
actually happens? What are you expecting to happen?

If your function OpenTour() is in the library js/virtualtour.js, then
why not do away with the href="javascript... and just do something
like:

<li><a class="pdflink" href="#" onclick="include_dom('http://www.hotel-
victoria.co.uk/js/
virtualtour.js');OpenTour('beach','beach_medres.ip x');return
false;">Beach</a><br /></li>
If you go to http://www.hotel-victoria.co.uk/virt...2/Default.aspx
and click on any of the links in the right panel, you will see what it
does when it works.

Oct 11 '07 #11

P: n/a
Captain Paralytic said the following on 10/11/2007 7:47 AM:
On 11 Oct, 12:33, RobG <rg...@iinet.net.auwrote:
>How does the above address the OP's issue of very large pages?
Well, the following 2 extracts from the referenced page explain this:
No, it attempts to explain it without explaining some of the problems
with the method of loading js files on the fly.
The problem
I was wondering how feasible it is to call Javascripts only when you
need them. Basically not to have a load of <script src="js.js">s at
the top of the page, but only those that are actually needed, when
they are needed.
And there is a problem with the approach used in that page that isn't
covered in that page. Namely, instead of a simple alert, try loading a
function in the external .js file and then calling that function. You
will run into a timing issue and that timing issue isn't covered in that
page.
Javascript include on user demand
Now a bit more of a real life example - the javascript is included
when the user performs an action (click).
If the only thing in the external file is an alert, then it will work.
Again, try putting a function in the external file and then calling it:

onclick="include_dom('someFileWithAFunctionInIt'); someFunctionInTheExternalFile()";

Even then, loading files on the fly isn't a solution to the OP's question.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 11 '07 #12

P: n/a
On Oct 11, 3:13 pm, Randy Webb <HikksNotAtH...@aol.comwrote:

[snip]
>
setAttribute is buggy in IE, set the property directly:
It isn't buggy, but broken as designed.
js.type="text/javascript";
This is the same as using setAttribute in IE, which is the whole
problem with IE's implementation. In this particular case it doesn't
make any difference as the element property is a string and the
attribute name is not a reserved word or camel-case.

On the other hand, these will all fail in IE:

el.setAttribute('disabled', 'disabled'); // disabled property is
boolean

el.setAttribute('style', 'color:red'); // style property is an object

el.setAttribute('onclick', 'alert("test");'); // onclick property
needs a function

el.setAttribute('for', 'myID'); // for is reserved, property is
htmlFor

el.setAttribute('maxlength', '10'); // property is maxLength

The getAttribute method has the same botched design. Of course, it is
rarely useful to call getAttribute to retrieve something that is
exposed by a standard DOM property in the first place.

The feature test for this is trivial, but a full workaround is not as
IE does weird things when setting the type property of input
elements. I got tired of the get/setAttribute taboo a while back and
worked out wrappers, but they are not pretty.

It is beyond belief that MS didn't change this in IE7.

Oct 12 '07 #13

P: n/a
David Mark said the following on 10/11/2007 8:20 PM:
On Oct 11, 3:13 pm, Randy Webb <HikksNotAtH...@aol.comwrote:

[snip]
>setAttribute is buggy in IE, set the property directly:

It isn't buggy, but broken as designed.
Agreed.
>js.type="text/javascript";

This is the same as using setAttribute in IE, which is the whole
problem with IE's implementation. In this particular case it doesn't
make any difference as the element property is a string and the
attribute name is not a reserved word or camel-case.

On the other hand, these will all fail in IE:
<snipped examples of IE's broken setAttribute>

That is why I don't use it at all. It is easier to just not use it at
all than it is to try to remember when it will - or won't - work properly.
The feature test for this is trivial, but a full workaround is not as
IE does weird things when setting the type property of input
elements. I got tired of the get/setAttribute taboo a while back and
worked out wrappers, but they are not pretty.
Is there a place that setting the property directly doesn't work?
It is beyond belief that MS didn't change this in IE7.
Totally agreed.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 12 '07 #14

P: n/a
David Mark said the following on 10/11/2007 9:27 PM:
On Oct 11, 8:44 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
[snip]
>Is there a place that setting the property directly doesn't work?

If the corresponding DOM property has problems (like the type property
of input elements), then a working setAttribute (or ugly workaround)
is a necessity.
That would explain why I have never had a need to use it. I have never
tried changing the type of an input element. If I wanted a different
type, I would simply remove the existing and replace it.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 12 '07 #15

P: n/a
Captain Paralytic said the following on 10/11/2007 11:27 AM:
On 11 Oct, 16:11, Stapes <steve.sta...@gmail.comwrote:
>Doesn't work.

"Doesn't work." How helpful. Care to share how it doesn't work? What
actually happens? What are you expecting to happen?

If your function OpenTour() is in the library js/virtualtour.js, then
why not do away with the href="javascript... and just do something
like:

<li><a class="pdflink" href="#" onclick="include_dom('http://www.hotel-
victoria.co.uk/js/
virtualtour.js');OpenTour('beach','beach_medres.ip x');return
false;">Beach</a><br /></li>
Why you shouldn't do something like that is one of the problems I have
mentioned in my other replies in this thread. You run into a timing
issue whereby the file virtualtour.js doesn't have time to download
before OpenTour is executed. You can test it locally and it will usually
"work" because of the loading time. Testing it from a remote server will
usually cause it to fail and IE/FF will throw an error that OpenTour is
not defined (and rightfully so since it isn't defined when you try to
call it).

This is also why I wrote that trying to dynamically load the file a
function in and then execute it is a bad strategy most of the time. In
fact, it is a horrible strategy.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 12 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.