jQuery Examples - How to use Custom Scripts?



It is better to write our custom code in the custom JavaScript file : custom.js, as follows −

/* Filename: custom.js */
$(document).ready(function() {
   $("div").click(function() {
      alert("Hello, world!");
   });
});

Now we can include custom.js file in our HTML file as follows −

Live Demo
<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>

      <script type = "text/javascript" src="/jquery/custom.js"></script>
   </head>
 
   <body>
      <div id = "mydiv">
         Click on this to see a dialogue box.
      </div>
   </body>
</html>

This will produce following result −

jqueryexamples_environment.htm
Advertisements