Where to save the JS from a site

0

When I develop a site, I always save it inside a folder called js , and the file name is usually acoes.js . What happens is that I call this file on each page. And sometimes, I create a function for a particular page, and when I'm in another, because there is neither class nor the id containing this function, / p>

I always have to check whether it exists or not, like this:

if($('#MINHA_ID').length>0){ MINHA FUNÇÃO});

I'm doing this check, that this id is being used or not on this page, no problem.

Is this the right way? or should I make a separate js for each page?

    
asked by anonymous 16.10.2014 / 14:21

3 answers

5

The ideal solution is to have an object with all the functions you need, you can have it in a single file. Alternative is to have functions in global space, but it is good practice not to pollute global space.

These functions should only be called when needed, rather than having mixed code running from one end to the other.

Whenever possible pass everything the function needs as a function parameter, there you will be giving all the ingredients you need without depending on what is in the DOM.

Example, inside your JS file:

var Site = {

    start: function(){
        Site.Cookie = new Hash.Cookie('selections-005', {duration: 365});
    },
    menu: function(li){
        // acões especificas do menu
    },
    uncheck: function(input, force){
        // outro método que gere input[type=checkbox] por exemplo
    },
    check: function(input){
        // outro método que gere input[type=checkbox] por exemplo
    },
    selectAll: function(){
        $('#download tr.check').each(Site.check);
    },
    selectNone: function(){
        $('#download tr.check').each(Site.uncheck);
    }
};

Site.start();
    
16.10.2014 / 14:32
2

I'm pretty sure most answers will be based on opinions, and that question may be closed. But ...

There is nothing wrong with doing this check if($('#MINHA_ID').length>0){ MINHA FUNÇÃO}); ... In fact I think it should always be done in content that is not common to all pages, such as header e footer .

Based on Facts

The advantage of creating js files for each page is that you do not need unnecessary data transmission, meaning you do not need to send a useless code block to the user. On computers this advantage seems to be "silly", but on mobile phones where users often have plans with transfer limit this is very valid .

Based on Opinion

In the old days I used to keep everything in one file, but today I believe that the best way to organize JavaScript files is to have a main file, for example main.js , for all pages and files specific to each page and I call them according to the page loaded, for example home.js or contato.js (but sometimes (for laziness) nor create the files, I leave them in the same page: <script>...blá blá blá...</script> ). And still have a function-only file, which I usually call util.js or funcoes.js .

    
16.10.2014 / 14:35
1

In fact this problem you are encountering has been quite discussed, and apparently the official solution to this problem is the use of web components .

I think in your case it's worth taking a look at polymer , and here in stackoverflow you also have a question about it.

    
16.10.2014 / 15:30