Best way to organize javascript functions and files? [closed] - javascript

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 9 years ago.
Improve this question
This title is giving me a warning about the question being subjective but I hope that's not the case.
Here's my situation: I have several functions(some are used on many pages, some only on one page) and I put them all into one .js file to save on load time. However, some pages run functions onload. For this to work, I need to have the functions file declared prior to the call. However, some of the functions require the page to be generated before it can grab the information it needs, so the file declare has to be at the end of the page. So right now, I've made two file declarations on the same page, for the same file. Looking in the console, this obviously causes problems as the first file can't get the information it needs, and throws an error.
So my question: Would it be best to break the functions in to two files (one pre-load one post?) or should the problem ones (which are functions unique to the page anyway) be hard coded at the top?

The best solution would be to break it into two files. That way you can reuse the code in more pages since the pre-load scripts work as they're supposed to, and the post-load scripts get the information they require. It would make the rest of you coding much easier.

Related

How to structure the JS? Put the code in a unique file or require the necessary JS code in the file where its used? [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 4 years ago.
Improve this question
Do you know if is better to have a unique file with all JS or is better in each file require the specific JS that is necessary for that specific page?
The project sould stay better structured requiring the JS specific for each file.
But in terms of performance do you know if is basically the same or not?
For a small JS snippets is always better to implement it only on pages you need.
But, for big classes, framework or huge functions is better use single file with an CDN. That way is better performance and finaly better to maitenance and for developing is better to stay organized.
As second, in single file you can easily do minified version fully automated.
I prefer creating a unique JavaScript file and then linking it to the html file. It is the most efficient and organized way of structuring your code. But if the amount of code is very small and only required for that single page, inline JavaScript is preferred.
Performance: External JavaScript is always faster because the browser can cache an external file but Inline JavaScript will always be loaded afresh and hence is slower.

Which way is better to call a jquery function twice? [closed]

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 7 years ago.
Improve this question
I have a javascript jquery function which I need to use twice, in 2 different files.
Now, the question is, which is the better way to implement it, performance-wise. Should I declare function global using 'window' variable, or should I declare the function in each of the files?
Please keep in mind that traffic is not an issue, the script will be used from the hard-drive.
Thanks for your help.
My answer follows the DRY Principle (Don't Repeat Yourself). If you put the function in both files, and later find a bug on Page #1, someone has to remember that the function is duplicated on Page #2 as well (in this case, assume the bug report says "Page #1 doesn't work properly"). From that bug report, would the developer know to also modify Page #2? To avoid the human error piece, I'd always recommend you don't copy/paste functions into multiple locations.
Performance wise, if you're not concerned about traffic, the difference is nanoseconds slower wrapping it in a shared function as you do have to create an additional stack frame for the shared function call that in turn calls jQuery, but we're really talking nanoseconds. For a few nanoseconds lost, I'd say DRY is the way to go.

Single javascript file vs MVC [closed]

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 8 years ago.
Improve this question
Im just getting into app development and it seems that the style is to use an MVC style of coding where you have an app.js, controller.js, and service.js and etc. All codepens I see are always a single HTML file plus a single js file. What is the difference between these two styles? It seems like building putting them all in a single js file would be more convenient but why would someone go through the trouble of splitting up the code?
The short answer: Codepen is a sandbox for testing, not app development. Putting all your code in a single file may be convenient for a simple example, but once you have to scroll through thousands of lines of spaghetti code, you'll soon change your mind. Putting your code into separate files, or modules, is worth the trouble; you can always minify before moving to a production environment.

What is the best practice using Dynamically generated forms or html imput tags [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 8 years ago.
Improve this question
Im doing a form where multiple tags appear when certain conditions in the request met, I wonder whether using jQuery to dynamically generate new HTML input tags, or doing all the HTML tags I need in the .jsp file. The first option means making a bigger .js file, the last means making a bigger .jsp file. Are there any advantages of one against the other?
Thanks for your time
It depends.
The main advantage to doing it server-side (jsp in your case) is that your forms remain accessible if the user has turned off javascript or your javascript is broken.
Integrations tests which test your forms will also run faster if you do not need to run a (headless) browser with a full javascript runtime.
However, single-page applications are by nature javascript-heavy. The server often only serves a simple html skeleton which is then populated by javascript. An advantage to this approach is that web apps can be made to feel very responsive since there is a minimal document and the user can start interacting which the page even as it loads.

add image CSS : local file or url? [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 8 years ago.
Improve this question
I am working on a HTML Webpage with CSS and Ajax. I would like to add an image and I know I can do it by adding the image in a folder inside my project or add it with an URL so which one is better method to use, I mean what should be the inconvinients of each method that would make me prefer one to the other ?
You should have it in a local file because:
1) Relative paths;
2) You have more control on it (what if the other site is down?);
3) You don't have to read 1000+ pages of terms of use in which the external website may say that they will own your images if you put them there.
You should use external hosting because:
1) It is cheaper (sometimes free);
2) You can free up bandwidth (especially if you have a lot of images);
3) Using relative paths is not always better (and can cause issues with migrations, see the comment)
Deciding which one you would use depends on your needs. If you have few images, it is better to store them locally. If you have a lot of images and don't have the resources to host them, it is better to use external services.

Categories

Resources