Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How/when/where to include external javascript

I'm looking for some advice on the best way to hold my JavaScript (jQuery) functions.

I am developing in MVC/razor and therefore have a layout page. I include my jQuery library and an external JavaScript file in here so it's available in every single page.

This is working well, but I am now becoming very aware of the fact that I am adding almost 300 lines of JS to EVERY page, where maybe half of that is used in any one of these pages.

One function is not in the external file and instead sits inside the HTML because I need to use variables set in my razor code.

I have a couple of questions around this arrangement:

  • Is placing JS inside the HTML generally acceptable when variables set using razor are used? There does not appear to be a clean way of passing a variable into an external js file
  • Should I split my functions down in to individual JS files and just include what is needed for each page in the site?
  • If I were to split them into multiple files, how would that work with jQuery's (document).ready ? Do I need to use that if all the JavaScript I am including is to be used?

I'm sure this will more a matter of opinion than a black and white answer, but I want to consider all my options before moving on. Even though it works fine as is, I can't help but feel there is a better/cleaner way.

like image 885
Alan Shortis Avatar asked Dec 19 '12 14:12

Alan Shortis


People also ask

Where should I put external JavaScript?

You can place an external script reference in <head> or <body> as you like. The script will behave as if it was located exactly where the <script> tag is located. External scripts cannot contain <script> tags.

Where is the right place to include JavaScript file?

JavaScript Learn JavaScript Quick Course Beginners </script>. To place JavaScript in an HTML file, use the <script>… </script> tag. You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is normally recommended that you should keep it within the <head> tags.

What is more appropriate way to include JavaScript as an external file?

Create external JavaScript file with the extension . js. After creating, add it to the HTML file in the script tag. The src attribute is used to include that external JavaScript file.

Does it matter where you put JavaScript?

If it is using the document. write() method, then it does matter where it is on the page. If it's trying to reference elements in the DOM, it is best put in the HEAD and have those functions that access DOM elements triggered after the page is loaded.


2 Answers

Remember once a user lands on your homepage and loads the javascript file it will be cached in their browser so subsequent pages will not download the Javascript again.

I would definitely keep the js separate, you could have a snippet on each page that initialise the JS that that particurlar view needs. Put something like the below in the views that need to run JS

$(document).ready(function() {
    mysite.mypage();
});

Then the function mysite.mypage() can be defined in the external JS file. 300 lines isnt the end of the world, I would say its probably too early to be worryign about optimisation.

You could always look at minifying that JS file to decrease the size. A quick and easy way to do this is here:

http://www.minifyjavascript.com/

like image 178
cowls Avatar answered Sep 27 '22 21:09

cowls


Have you ever heard of require.js? http://requirejs.org/ I find it really useful.

It's a module loader so you are able to split all of your JS code into individual files and load only the ones you need on each page.

I don't know about passing a variable to an external JS file, I don't think its possible / the 'right' way.

You can make each external JS file into a function that accepts and returns parameters. Then in the page you need to use it: - include the file dependancy - call the function

Thats what I do, seems like your 2nd suggestion.

for the $(document.ready) question its really up to you. You don't have to use it but its useful for some things , check out this overview: http://docs.jquery.com/Tutorials:Introducing_$(document).ready()

like image 21
Peadar Avatar answered Sep 27 '22 19:09

Peadar