views:

40

answers:

3

Hello, I have a jquery-code which using selectors (from current document). Now I want to move this code into another file(js) and just include from this document. Could I do it without any editing? So, will jquery-code works right?

+2  A: 
<script src="jquery.js"></script>
<script src="main.js"></script>

in the head or right before the end body tag will do. If this is in the head then make sure you're using $(document).ready( fn ).

meder
A: 

Yes, all the selectors you currently use are relative to the DOM of the web page/document that you are working on. Whether the javascript code that produces and uses these selectors is embedded in the html page or included from an external js file is not relevant. (Though this latter layour is much preferable approach for other reasons)

mjv
A: 

To quote and expand on meder's answer

<script type="text/javascript" src="/SCRIPT_DIR/jquery.js"></script>
<script type="text/javascript" src="/SCRIPTS_DIR/main.js"></script>

Would be a better way to declare the javascript. Note the script type is specified and the src URL starts with a / character which specifies an absolute reference to the script file, which is good practice for nested pages.

Evildonald