Bob
Bob

Reputation: 119

Loading a script in the <body> section

I have a javascript for a specific page that I do not wish to be loaded in my header section. Is it possible to load it in the section of the HTML.

Currently I have all my js code inside the but I want to remove it to a seperate js file that I can load.

I tried using this but it did not work.

<script type="text/javascript" src="<?php echo base_url();?>js/jquery-1.5.1.min.js"></script>

Thanks

Upvotes: 5

Views: 25430

Answers (6)

Praveen Prasad
Praveen Prasad

Reputation: 32137

Q1 : I have a javascript for a specific page that I do not wish to be loaded in my header section. Is it possible to load it in the section of the HTML.

-Yes you can load javascript any where you want, if writing inline code then make sure you add script tag around your code.

-also you can request files like in body

Q2: Currently I have all my js code inside the but I want to remove it to a seperate js file that I can load.

-- no problem in that, thats even better practice.

Q3 Requesting external file

to request external files you write below written fashion

<script src="http://file_name.js" type="text/javascript"></script>

Upvotes: 9

T.J. Crowder
T.J. Crowder

Reputation: 1075925

It's not only possible (ref), it's frequently a good idea.

Putting your scripts as late in the page as possible, which frequently means just before the closing </body> tag, means the browser can parse and display your content before stopping to go download your JavaScript file(s) (if external) and fire up the JavaScript interpreter to run the script (inline or external).

Putting scripts "at the bottom" is a fairly standard recommendation for speeding up the apparent load time of your page.

Upvotes: 7

Saeed Neamati
Saeed Neamati

Reputation: 35852

Yahoo engineers recommendation for higher performance is to include your scripts at the end of your HTML, just before </body> tag. Therefore, it's even better.

To see where the problem is, you gotta first make sure that your js file is loading. User Firebug and go to scripts tab. Do you see your script? If not, then something is wrong with your path.

Upvotes: 1

Quentin
Quentin

Reputation: 944568

Is it possible to load it in the section of the HTML.

Yes.

From the spec:

<!ELEMENT BODY O O (%block;|SCRIPT)+ +(INS|DEL) -- document body -->

SCRIPT is among the elements that may be a child of the BODY elements. Numerous other elements may also have SCRIPT children.

<script type="text/javascript" src="<?php echo base_url();?>js/jquery-1.5.1.min.js"></script>

When I run echo base_url() I get my the hostname of my server. This would result in a URL such as example.comjs/query-1.5.1.min.js. You probably should drop that PHP snippet entirely and just use: src="/js/jquery-1.5.1.min.js" which would resolve to http://example.com/s/query-1.5.1.min.js.

Upvotes: 1

Variant
Variant

Reputation: 17385

it should work...

Did you try to view the generated source and see if the PHP code indeed generated the right path?

beside that, it is recommended to load jQuery from a CDN such as google's : https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js

Upvotes: 0

Ibu
Ibu

Reputation: 43850

Yes it is possible. Try and see.

For debugging, hardcode the jquery full path.

It is sometime recommended to load it at the end of the of the body, to make the main content of the page load faster.

Upvotes: 1

Related Questions