Is it possible to create a JS file for all pages?

0

I have several pages, I wonder if you can use the same JS for the same page.

For example:

  

I have a HOME page, where I load the "child" pages within a% content, but for each page, I need to create a new JS

$("#lista_cli").click(function(){
    $('#content').css('display', 'none');
    $("#content").load('lista_clientes.php');
    $("#content").fadeIn(2000);
});

$("#cadastro_cli").click(function(){
    $('#content').css('display', 'none');
    $("#content").load('cadastra_cliente.php');
    $("#content").fadeIn(2000); 
});

I've been doing some testing, at some point, when I give div to the JS file of the home page in a content of the other page (after it loads), it shows me a ALERT correctly, but AJAX requests do not work. Maybe it was kind of confusing, but basically:

Can you use the same JS on multiple pages? is it recommended?

    
asked by anonymous 11.12.2017 / 04:02

1 answer

0

If the code in the JS file is common to more than one page, you can certainly reuse it, not only can it, but this is one of the main advantages of separating the javascript code into a separate html file.

To use the same file on more than one page, simply import it into your html pages eg:

<script src="meuScript.js"></script>
    
11.12.2017 / 04:27