2

I have a jQuery library code in jquery.xyz.js . I have an html file which uses the function defined in jquery.xyz.js in this manner .

<html>
<body>

<script type="text/javascript">
document.write("This is my first JavaScript!");
$(function(){ $("ul#xy01").xyz(); }); 
</script>

</body>
</html> 

But the jQuery is not running, which I am assuming because I haven't loaded the jQuery properly onto the html page. So how do I do it?

6 Answers 6

4
 <script type="text/javascript" src="jquery.js"></script>

See how jQuery works in the manual for the basics, and the download page to fetch the library (or to find out addresses for direct linking on a Content Delivery Network).

Sign up to request clarification or add additional context in comments.

Comments

1
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js">   
</script>
</head>
<body>

<script type="text/javascript">
document.write("This is my first JavaScript!");
$(function(){ $("ul#xy01").xyz(); }); 
</script>

</body>
</html> 

Comments

1

You just need to include the script files before using functions defined in them ($ is just a function), for example:

<html>
<body>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript" src="jquery.xyz.js"></script>
  <script type="text/javascript">
    $(function(){ $("ul#xy01").xyz(); }); 
  </script>
</body>
</html> 

Be sure to include jQuery before plugins that rely on it, or you'll get some errors first thing.

Comments

1
<script>

var script = document.createElement('script');
script.onload = function () {
 //do stuff with the script

};
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/xx.xx/jquery.min.js';
document.head.appendChild(script);

</script>

Comments

0
<script type="text/javascript" src="PATH TO YOUR JS FILE"></script>

Stick this above your jQuery code in the <head></head>

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.