0

I am new to javascript and jquery. I want to use $(document).ready to register some event handlers. For example, code like this, and my question is what javascript libraries do I need to refer to at the head of the page in order to use $(document).ready? Appreciate if anyone could provide me a simple easy to use sample to learn $(document).ready.

<script>$(document).ready(function()
{
// function implementation internals
});
</script>

thanks in advance, George

4 Answers 4

2

All you need is the jQuery library.

http://www.jquery.com

You can either download the library and include it from your own server or you could use one of the many CDN's which provide the library. For instance:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

<script type="text/javascript">
     $(document).ready(function() {
           // do something useful
     });
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, a bit confused. Could you let me know what code do I need to use to refer?
@George2 Download and include the js file from docs.jquery.com/Downloading_jQuery
@Sri Kumar: That works; Andy's version using Google's CDN can be faster though: stackoverflow.com/questions/547384/…
@Piskvor Cool! I am not aware of it Thanks! Yet, What if the third party server goes down? Still, I know that Google server going down is very very rare :)
@Sri Kumar: Since it's not a single server, but a worldwide CDN, it's much more likely that your own server goes down. But yes, in the unlikely case the CDN is down, the script couldn't be loaded.
|
1

Google keeps copies of a bunch of libraries on their servers, which are pretty reliable.

Just add the following to your <head> section, and place your snippet somewhere below.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>

1 Comment

I wouldn't necessearily recommend to add script tags into the <head> section. Probably a better idea to put those at the end of the <body> tag.
1

In summary,

Put the <script> tag provided by sje397 in the <head> section of the page, which provides the only library you need... jQuery.

(Alternatively: <script src="http://code.jquery.com/jquery-1.4.4.js" type="text/javascript"></script>)

Read http://docs.jquery.com/Tutorials:How_jQuery_Works

And you should be good to go.

Comments

1
<html>
<head>
</head>
<body>

<div id="someElement">Click Me</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    (function($) {
        $(document).ready(function() {

            $('#someElement').bind('click', function(event) {
                // event.preventDefault(); // you might want to do this if your event handler has a default action associated with it (e.g. a link that gets clicked with an href)
                // do stuff on your event (change click to whatever you need)
            });

        });
    })(jQuery);
</script>
</body>
</html>

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.