document ready function:
You might have noticed that all jQuery methods, in our examples, are inside a document.ready() function:
$(document).ready(function(){ //jQuery functions go here.. });This is to prevent any jQuery code from running before the document is finished loading(is ready).
Here are some examples of actions that can fail if functions are run before the document is fully loaded:
- Trying to hide an element that doesn’t exist
- Trying to get the size of an image that is not loaded
How to use custom scripts?
It is better to write our custom code in the custom javascript file: custom.js, as follows:
/* file name: custom.js */ $(document).ready(function(){ $(“div”).click(function(){ alert(“hello world!”); }); });
Now we can include custom.js file in out HTML file as follows:
<html>
<head>
<title>The jQuery Example</title>
<script type=”text/javascript”
src=”/jquery/jquery-1.3.2.min.js”></script>
<script type=”text/javascript”
Src=”/jquery/custom.js”></script>
</head>
<body>
<div id=”newdiv”>
Click on this to see a dialogue box.
</div>
</body>
</html>
Comments/Suggestions are invited. Happy coding......!
Comments Post a Comment