Solved

how to include jquery.js in another js file

Posted on 2014-11-03
18
426 Views
Last Modified: 2014-11-11
how to include jquery.js in another js file
0
Comment
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40420942
is this code correct ?

<script type="text/javascript" src="jquery.js"></script>
0
 
LVL 1

Expert Comment

by:Chaitanya Dhareshwar
ID: 40420957
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
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40420978
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 22

Expert Comment

by:Mrunal
ID: 40420981
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
 
LVL 1

Expert Comment

by:Chaitanya Dhareshwar
ID: 40420985
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
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40421017
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
 
LVL 1

Expert Comment

by:Chaitanya Dhareshwar
ID: 40421023
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40421083
No matter how you go about it, if your script uses jQuery, then jQuery must be loaded First.
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40421132


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

Author Comment

by:Sathish David Kumar N
ID: 40421135

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

Expert Comment

by:Dave Baldwin
ID: 40421216
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
 
LVL 1

Expert Comment

by:Chaitanya Dhareshwar
ID: 40421258
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
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40423208
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
 
LVL 1

Accepted Solution

by:
Chaitanya Dhareshwar earned 500 total points
ID: 40423288
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
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40423385
<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
 
LVL 1

Expert Comment

by:Chaitanya Dhareshwar
ID: 40423393
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
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40434449


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

Expert Comment

by:Chaitanya Dhareshwar
ID: 40434490
change the text where it says "<PATH OF FILE>" and put in the correct path of the file :)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to deal with the query string 8 37
Google Analytics setup 5 50
jQuery dialog box help 2 17
Asp in server side with Mssql Server 7 4 27
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question