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

S

Stapes

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
 
M

matatunos

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
 
C

Captain Paralytic

Stapes vient de nous annoncer :






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?
 
S

Stapes

Stapes vient de nous annoncer :






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
 
M

matatunos

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")
 
R

riog

Hi,

see http://www.phpied.com/javascript-include/

1. On your HTML page:

<script type="text/javascript" src="includes.js"></script>

2. create includes.js containing:

<script type="text/javascript">
function include_dom(script_filename) {
var html_doc = document.getElementsByTagName('head').item(0);
var js = document.createElement('script');
js.setAttribute('language', 'javascript');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', script_filename);
html_doc.appendChild(js);
return false;
}

include_dom("js/includes.js");
// add more javascript files here
</script>
 
R

RobG

Hi,

seehttp://www.phpied.com/javascript-include/

1. On your HTML page:

<script type="text/javascript" src="includes.js"></script>

2. create includes.js containing:

<script type="text/javascript">
function include_dom(script_filename) {
var html_doc = document.getElementsByTagName('head').item(0);
var js = document.createElement('script');
js.setAttribute('language', 'javascript');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', script_filename);
html_doc.appendChild(js);
return false;
}

include_dom("js/includes.js");
// add more javascript files here
</script>

Why would you bother to use a problematic script to load script files
that could be loaded more reliably using script elements in the source
HTML?

How does the above address the OP's issue of very large pages?
 
C

Captain Paralytic

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).
 
S

Stapes

Hi,

seehttp://www.phpied.com/javascript-include/

1. On your HTML page:

<script type="text/javascript" src="includes.js"></script>

2. create includes.js containing:

<script type="text/javascript">
function include_dom(script_filename) {
var html_doc = document.getElementsByTagName('head').item(0);
var js = document.createElement('script');
js.setAttribute('language', 'javascript');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', script_filename);
html_doc.appendChild(js);
return false;
}

include_dom("js/includes.js");
// add more javascript files here
</script>

That looks good. How do I add the javascript files where it says //
add more javascript files here ?

i.e. js/dnncore.js

Stapes
 
C

Captain Paralytic

That looks good. How do I add the javascript files where it says //
add more javascript files here ?

i.e. js/dnncore.js

Stapes- Hide quoted text -

- Show quoted text -

I would use the keyboard to type:
include_dom("js/dnncore.js");

But if you just do that, you will load all the files as you are doing
using teh script tags and so you have no improvement.

You need to look at the style for http://www.phpied.com/files/jinc/test5.html
 
S

Stapes

I would use the keyboard to type:
include_dom("js/dnncore.js");

But if you just do that, you will load all the files as you are doing
using teh script tags and so you have no improvement.

You need to look at the style forhttp://www.phpied.com/files/jinc/test5.html

Use the keyboard eh? Never thought of that.

Seriously though, http://www.phpied.com/files/jinc/test5.html uses:

<li style="cursor: pointer" onclick="include_dom('1.js')">Click for
1.js using DOM</li>

Suppose I want it to load automatically (i.e.without anybody clicking
anything). How would I do that?

Stapes
 
C

Captain Paralytic

Use the keyboard eh? Never thought of that.

Seriously though,http://www.phpied.com/files/jinc/test5.htmluses:

<li style="cursor: pointer" onclick="include_dom('1.js')">Click for
1.js using DOM</li>

Suppose I want it to load automatically (i.e.without anybody clicking
anything). How would I do that?

Stapes- Hide quoted text -

- Show quoted text -


Well, as I read your original post, your concern was that you only
wanted a script library to load when it was required on the particular
page.

If that is correct, then you have to come up with some way of defining
what scripts you need on any particular page and then some way of
loading them.

I was not suggesting that you used the text of test5 per se, only that
youuse the concept of loading only what you need, based on some
criteria.

How you do that has to be based on what is available in these "skins".

In short, people here can give you many ideas of how to handle this,
but we cannot write it all for you without knowing much more about
what is behind it all (and we might want payment too :)
 
T

Tim Slattery

Stapes said:
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.
 
C

Captain Paralytic

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.

How is that relevant to this discussion?
 
S

Stapes

Well, as I read your original post, your concern was that you only
wanted a script library to load when it was required on the particular
page.

If that is correct, then you have to come up with some way of defining
what scripts you need on any particular page and then some way of
loading them.

I was not suggesting that you used the text of test5 per se, only that
youuse the concept of loading only what you need, based on some
criteria.

How you do that has to be based on what is available in these "skins".

In short, people here can give you many ideas of how to handle this,
but we cannot write it all for you without knowing much more about
what is behind it all (and we might want payment too :)

I am not asking you to write it all for me! Jeezuss. This is just a
little tiny part of the whole. I am trying to work out why the method
given did not work.
I did say I have no experience of javascript. Gimme a break.

Can I put include_dom("js/dnncore.js") in other places - apart from in
an onclick= command?

Does it work out the rest of the url, or must I put "http://www.hotel-
victoria.co.uk/js/dnncore.js" or something?
 
S

Stapes

I am not asking you to write it all for me! Jeezuss. This is just a
little tiny part of the whole. I am trying to work out why the method
given did not work.
I did say I have no experience of javascript. Gimme a break.

Can I put include_dom("js/dnncore.js") in other places - apart from in
an onclick= command?

Does it work out the rest of the url, or must I put "http://www.hotel-
victoria.co.uk/js/dnncore.js" or something?

Ok here goes. On http://www.hotel-victoria.co.uk/virtualtour/tabid/72/Default.aspx,
I want to use virtualtour.js only on this page.

I have tried this:

<li style="cursor: pointer;" onclick="include_dom('http://www.hotel-
victoria.co.uk/js/virtualtour.js')"><a class="pdflink"
href="javascript:OpenTour('beach' said:

And in the default.aspx:

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

Doesn't work. So what am I doing wrong? I know it isn't the bit in
default.aspx, because all the other javascript functions loaded there
work fine.

Stapes
 
T

Thomas 'PointedEars' Lahn

Stapes said:
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.

Put them into the `body' element, where they are used, before they are used,
in the correct order. That is the only other place where `script' elements
are allowed.


HTH

PointedEars
 
C

Captain Paralytic

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.ipx');return
false;">Beach</a><br /></li>
 
C

Captain Paralytic

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
 
S

Stapes

"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.ipx');return
false;">Beach</a><br /></li>

If you go to http://www.hotel-victoria.co.uk/virtualtour/tabid/72/Default.aspx
and click on any of the links in the right panel, you will see what it
does when it works.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top