-4

i want create an program using javascript, but i want my file be more organize, is there some way that i can implement the concept of include or require of php. example: javascript file that only contains function let say function.js, then another javascript file main.js. is there a way that in main.js i can call a function in the function.js?

function.js


function test(){
   alert("hello world");
}

main.js


$(document).ready(function(){
    test();
});

Axel Roz
  • 45
  • 7
  • 5
    just do what you say, it works as you expect. – sadrzadehsina Dec 22 '14 at 15:14
  • Yes this works. If you want structure you should look at jquery Plugins: http://learn.jquery.com/plugins/basic-plugin-creation/ and create your own plugin (seperate structure from implementation) – Joel Harkes Dec 22 '14 at 15:15

1 Answers1

2

Yes, you can always use functions out of another javascript file.

though if you executed the code immediately you need to specify the right order: First the function definition, then the function call so:

<head>
    <script src="javascript.js" type="text/javascript"></script>
    <script src="main.js" type="text/javascript"></script>
</head>
SuperJulietta
  • 6,801
  • 1
  • 14
  • 11
Eun
  • 4,146
  • 5
  • 30
  • 51
  • 2
    Not per see because document ready will be called after all hole DOM is loaded (and all javascript) (if javascript added in header) – Joel Harkes Dec 22 '14 at 15:16
  • 3
    @joelharkes — The timing of ready events doesn't seem relevant to the answer. – Quentin Dec 22 '14 at 15:18
  • @Quentin Thanks what I think too, just the structure is important for the OP – Eun Dec 22 '14 at 15:19