Jitendra Vyas
Jitendra Vyas

Reputation: 152787

Can we use any type of javascript code as a external .js file or sometime it's necessary to place in <head>?

Can we use any type of javascript code as a external .js file or sometime it's necessary to place in <head>?

Upvotes: 0

Views: 137

Answers (4)

Rookie Programmer Aravind
Rookie Programmer Aravind

Reputation: 12154

Unless it is a Dynamic content which is placed by the server side scripts (very rarely used as there are many more better alternative methods) .. You can use JS in an external file ..

External js is re-usable .. I mean can be used by more than one HTML page .. So obviously it brings down the burden on browser ..

The site providing the live telecast or the NEWS/information (example:cricket scores etc) real-time examples for Dynamic content ..

Upvotes: 0

James Anderson
James Anderson

Reputation: 27478

The only time you would ever need to inline a js function in your HTML using the <SCRIPT> tags is if your javascript is generated by your server side program depending on the data, user settings etc.

Even this case is extemely rare as as you should be able to create a .js function whose behaviour is controlled by passing parameters.

Apart from keeping everything tidy and in the place where you expect to find it, there is a network performance advantage in that *.js files are cached on the client side so you are not constantly sending the same stuff over the network again and again.

Upvotes: 2

Brandon Henry
Brandon Henry

Reputation: 3719

if using jquery then $.document.ready() is the way to go

Upvotes: 0

Sampson
Sampson

Reputation: 268424

You can place it all in an external file. It's much cleaner, and easier to maintain. It's a good practice to keep Javascript and CSS in their own external files. Do away with inline switching between CSS, HTML and Javascript for a much better organized project and less frustration down the road.

Upvotes: 0

Related Questions