When does a website require multiple HTML files? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Are all the HTML files linked to the same JavaScript file, or do you need multiple JavaScript files? Is there ever a point where you need multiple CSS files?

When it grows large enough :)
Simple websites might only need one HTML file and no separate JS or CSS files. If a site has multiple pages that are returned from the web server, they would need to be separate HTML files (or separate rendered templates, etc.).
If a website or web app has enough JS and/or CSS that it's inconvenient to keep in one file, either because of the load time or the annoyance of editing a file that large, or the complexity of knowing which parts of the file go with which pages or components or templates, that's a good time to split those files in to multiples.
Most modern rich web applications aren't edited/written in terms of the specific files that get served to the browser anymore, instead those end-result files are produced by a bundler like Webpack, FuseBox, etc., into either single or multiple JS (and possibly CSS) files which are dynamically loaded by other internal JS code when they need to be. Smaller and simpler/less interactive websites might have multiple HTML files (or rendered templates) that share the same JS and CSS.

Related

HTML includes without js? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I am writing html for a web portal for my boss and using css (of course) for overall formatting.
First, he told me NOT to use css but to use html includes. My understanding is that html includes are used for html repeated across multiple pages, not for formatting. Also, in my understanding, html includes ALWAYS require js to insert them. Am I wrong? I would be happy to be wrong. I have a day of coding and markup ahead of me, I would like to start it making sense.
There is no such thing as an "HTML include".
There was a proposal for HTML imports but it is dead in the water.
You've tagged this ssi which is a server-side include (which is a standard include mechanism that some HTTP servers support; Apache HTTPD documents it).
Other mechanisms for including HTML include preprocessors like static site generators, templates and include mechanisms provided via server-side programming, and client-side mechanisms involving JavaScript.
The closest HTML itself has to an include is the iframe which lets you embed entire HTML document in a scrolling box in the page. (You can achieve similar effects with object and embed but I'm yet to hear a good reason to prefer them to iframe).
CSS is the correct tool for presentation, but sometimes you need some common HTML scaffolding to apply the CSS to, and sometimes you want to have a single place to include all your presentation meta data (e.g. <link> elements) that gets shared between pages.

What is better performance wise, 1 huge js and css file or multiple files [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Right now, I work on multiple css and js files but I combine them all together in 1 big file. I am not sure if this is the right way. For example my js file is about 200kb(Foundation + jQuery). This file is only loaded at the end of the body, so I am not sure what is the best way, I mean the file size won't get smaller anyway if I split them up.
You should always combine your CSS and JS files into a single file per type as a general rule.
However, if you really want to play around with load optimization, you can try to take advantage of the amount of simultaneous downloads a browser can do at one time. If it can do 6 at a time, then you won't see any major performance hit, and you may even get a performance gain, for having up to 6 separate files so they can download in parallel.
I personally wouldn't count on the simultaneous downloads though. It is a better rule of thumb to just combine them.
You can always use Googles PageSpeed to review your site for load optimization.
You should always combine and minify CSS and JS files. The browser makes fewer a separate request for each file you have. That's a small hit, but if you have a lot of files, it adds up.
https://developers.google.com/speed/docs/insights/MinifyResources
https://blog.hubspot.com/marketing/reduce-http-requests
Task runners like Grunt and Gulp can help with this. You can set up a script to minify and combine (concat) your files all in one action.
Or your IDE might even have it built in, so that everytime you save, a new file is compiled.
Always make sure your JS is right before the closing body tag, unless you have a specific reason to put it higher on the page.
The best solution for optimizing a website is firstly
1. To reduce the number of files to be loaded which reduces the number of browser requests,
2. To use server cache, to removing the repetition of MySql queries or connections
3. To use browser cache for the bigs javascript files
4. Synchronous and asynchronous requests or Ajax/Jquery

load all scripts in _Layout.cshtml as one bundle? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm writing a web tool using C#, HTML and JavaScript code based on the MVC pattern and I was wondering if it is smart (page load time, traffic, etc.) to load all needed scripts in the _Layout.cshtml.
Is there a difference in bundling all scripts together and load them in the head section of the _Layout.cshtml or load them in the actual views when needed?
Is there a best practice for this matter?
The best strategy is to "load what you really need". You should bundle all shared scripts and styles on one side and load that on the layout, and then create separate bundles for each view to load only what will be used on that particular page.
The difference would be that you will require more bundling configuration (as you would have one bundle per view), but IMHO, you will gain a lot more in a better code organization and avoid downloading unnecessary files.
Of course this all depends of what kind of application you are developing. But if you have all in one bundle and the project gets larger, it will be very difficult to later change the strategy as you will need to review all the dependencies on each view.
In terms of page load/traffic, it is clear that this strategy is better as the client will download only the files which are needed.
you can load script bundles in a view like this
#section scripts {
#Scripts.Render("~/bundles/movie")
}

Javascript - Questions about performance [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have some questions about Javascript.
Most of my website's pages use js. For better performance, what should i do ? :
Write all of my js code in one file and link it with my pages ? Or one js file per page ?
Is it a bad manner that i never mix js into my html ? I like to have separate things (html, css, js)
Thank you.
For maintainability
Keep separate pages while developing the app, so that any one can make out which feature has been coded where.
For performance
Before deploying it for production, minify your js and css files so that there are less network calls to download those files.
Is it a bad manner that i never mix js into my html ?
No, having non-intrusive js is both good for readability and it is good for performance too since it gives you a chance to minify the js files. You can't minify inline js.
My experience is writing a relatively big website in ASP.MVC .
I did not write all the javascript code in one file because that would have been difficult to manage. I made a folder that included multiple javascript files, each having its own purpose.
If the javascript is more than say 10 lines of code, and you can put it outside the html file, i would advise so, because it is faster to look at .
It is easier to just have one .js file for all pages, but it is better for readability if you have multiple files.
Mixing javascript into HTML can be useful but it depends on your needs.

What are some advantages and disadvantages for embedding JavaScript in HTML or saving it externally? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for simple bullet point answers please. I've tried looking all over, Googling, other questions here but I can never find both advantages and disadvantages for each method.
This is the answer I got from W3Schools pertaining to external javascript files
Pros
It allows separation of concerns - which is not a big deal in simple pages but as the script grows larger you can have a monolithic html page. Big files in general are not ideal for maintainability
It allows caching - when the browser loads a script externally (whether it's be from your site or a cdn) it caches the file for future use. That's why cdn's are preferred for commonly used scripts. Makes the browser use a cached script instead of building a new one every time the page loads which makes the page load faster
More readable code - this ties into the first bullet point but nevertheless it is important. The smaller the files we humans are working with the better. It is easier to catch mistakes and much easier to pass of the torch to the next developer working on the project or learning from it.
Cons
The browser has to make an http request to get the code
There may be other browser specific reasons as well, but I believe the main reason is the separation of code into different components.
Probably the best advantage of using external javascript files is browser caching - which gives you a good performance boost.
Imagine you have a site that uses MyJsFile.js (a random 50kb javascript file that adds functionality to your websire).
You can:
embed it in every page, and add the 50kb to every page request (not ideal)
link it in every page (<script src="MyJsFile.js"></script>)
The second option is usually prefered because most modern browsers will only get the file once, and serve it from the browser cache instead of downloading it at every request.
Check out similar questions:
Why not embed styles/scripts in HTML instead of linking?
When should I use Inline vs. External Javascript?
Is it better to put the JS code on the html file or in an external file?

Categories