Link to home
Start Free TrialLog in
Avatar of Sathish David  Kumar N
Sathish David Kumar NFlag for India

asked on

how to include jquery.js in another js file

how to include jquery.js in another js file
Avatar of Sathish David  Kumar N
Sathish David Kumar N
Flag of India image

ASKER

is this code correct ?

<script type="text/javascript" src="jquery.js"></script>
You can use httpcombiner if you are using a .net platform.

http://archive.msdn.microsoft.com/HttpCombiner 

Or optionally you can use yui compressor
http://developer.yahoo.com/yui/compressor/ 

Or Google's Closure compiler
http://code.google.com/closure/compiler/
https://github.com/dfsq/compressJS.sh

Requirejs can also help
http://requirejs.org/docs/optimization.html

Manually combining the files is another option... Just copy all the code together into one file, keep a few lines gap between each file's contents.

But you need to be especially careful with closing tags or comments, plus remember with any manual process its possible to have errors. If you want to try the manual option and need help please feel free to reply and I can assist with the same.
sorry my question is .

i want to use jquery funtion in my x.js file so i need to include jquery.1.11.0.js in my x.js .

how to do thAT??
Include jquery file in a page(html/aspx/ascx) where you have included x.js
so that jquery file will be already loaded on browser when you accessed jquery functions in your x.js file.

Further you have have intellisense of jquery fucntions.
For this just drag your jquery file into x.js. It will automatically added the reference.

Hope this helps you.
In that case what you've written is quite correct. You can refer to more details on how to use Jquery at http://www.w3schools.com/jquery/jquery_intro.asp 

Basically for code in one javascript file to use jquery you just need to give the reference in your HTML.
i think my question is confusing ....

lets take in this way

x.js and y.js file is there i want to include

y.js in x.js  

how to do that ??
It depends on how you wish to use it. Let me elaborate.

1. If you plan to use both scripts in the same HTML page (which is what I assumed) - you can just include x.js and y.js on the same HTML page and they can use/invoke functions within each other.

2. If you want to run the javascript file via CScript or WScript - in which case jquery tends to malfunction - you can use something like:

var Fs = new ActiveXObject("Scripting.FileSystemObject");
var Lib = eval(Fs.OpenTextFile("include.js", 1).ReadAll()); // where include.js is the file containing the functions you wish to invoke
WScript.echo(Lib.hello()); /* greetings! */


So end of the day it comes down to how you wish to USE it. If you can elaborate usage further we can find more options, maybe simpler options :)

Thanks,
C
No matter how you go about it, if your script uses jQuery, then jQuery must be loaded First.


No matter how you go about it, if your script uses jQuery, then jQuery must be loaded First.
How to load that one  ??

Chaitanya Dhareshwar thanks for your answer .

I can include both the files in my JSP but one js is used by lot of JSP's . If i add jquery function in js that will effect the other JSP files so if i include jquery.js in my local js then it will not get reflected . thats why
Actually, I don't think you should try to do this.  You can combine other scripts into one file but you should ALWAYS load jQuery first.  If you do not, you will find that the scripts that use jQuery functions will Not run.
Jquery from my experience is something that needs to be used as part of a web page. Client side - in a browser. If your javascript is running from anything other than a browser, probably jquery is not the right choice for you.

1. Locate a plain javascript plugin that does the same task
2. Write a custom plugin or get a JS developer to help write one
3. Use node.js if you need jquery functionality server side; however this can be tricky so only choose this option if you have adequate spare time

Loading mechanism for any javascript is pretty easy - that is less relevant that your end-result objective.

Answer these 2 questions for me please:

1. Where do you want to run your final javascript file?
2. Which browser will you use?

Thanks
1. Where do you want to run your final javascript file?----- from my browser only(client)
2. Which browser will you use?----------- crome,firefox
ASKER CERTIFIED SOLUTION
Avatar of Chaitanya Dhareshwar
Chaitanya Dhareshwar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<head>
<script type="text/javascript" src="<PATH OF FILE>jquery.js"></script>
<script src="<PATH OF OTHER FILE>Y.js"></script> <!-- this is your other script file that uses jquery -->
</head>

Open in new window


is i want to add this code in JS file ??
No this needs to go in the HTML. But you need to set the correct the paths for jquery and the other javascript file first.


No this needs to go in the HTML. But you need to set the correct the paths for jquery and the other javascript file first.

How ??
change the text where it says "<PATH OF FILE>" and put in the correct path of the file :)