how to include jquery.js in another js file

how to include jquery.js in another js file
LVL 20
Sathish David  Kumar NArchitectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sathish David Kumar NArchitectAuthor Commented:
is this code correct ?

<script type="text/javascript" src="jquery.js"></script>
0
Chaitanya DhareshwarHead IT and OperationsCommented:
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.
0
Sathish David Kumar NArchitectAuthor Commented:
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??
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

MrunalCommented:
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.
0
Chaitanya DhareshwarHead IT and OperationsCommented:
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.
0
Sathish David Kumar NArchitectAuthor Commented:
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 ??
0
Chaitanya DhareshwarHead IT and OperationsCommented:
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
0
Dave BaldwinFixer of ProblemsCommented:
No matter how you go about it, if your script uses jQuery, then jQuery must be loaded First.
0
Sathish David Kumar NArchitectAuthor Commented:


No matter how you go about it, if your script uses jQuery, then jQuery must be loaded First.
How to load that one  ??
0
Sathish David Kumar NArchitectAuthor Commented:

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
0
Dave BaldwinFixer of ProblemsCommented:
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.
0
Chaitanya DhareshwarHead IT and OperationsCommented:
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
0
Sathish David Kumar NArchitectAuthor Commented:
1. Where do you want to run your final javascript file?----- from my browser only(client)
2. Which browser will you use?----------- crome,firefox
0
Chaitanya DhareshwarHead IT and OperationsCommented:
Ok. If you need to use in a browser then the method you mentioned was correct.

<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>

Be sure to check what the <PATH OF FILE> is.

However jquery may NOT load if you double-click the web page to run it specially in Chrome. The file needs to be served from a browser. You would want to use a local webserver like XAMPP or IISExpress to run it (ref: http://stackoverflow.com/questions/17947971/ajax-in-jquery-does-not-work-from-local-file)

I feel this is is largely the solution to the query you've noted here - please confirm whether this works for you else we'll locate other possible solutions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sathish David Kumar NArchitectAuthor Commented:
<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 ??
0
Chaitanya DhareshwarHead IT and OperationsCommented:
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.
0
Sathish David Kumar NArchitectAuthor Commented:


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 ??
0
Chaitanya DhareshwarHead IT and OperationsCommented:
change the text where it says "<PATH OF FILE>" and put in the correct path of the file :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.