0

I want to use jquery in my django project but I can't seem to be able to load the jquery file as my simple function just doesn't do anything.

I put the jquery.js file in the same folder than my view and I call it this way

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

But then when I try a simple function, it does nothing when I click on a link

<script type="text/javascript">
$(document).ready(function(){
    $("a").click(function(){
        alert("mii");
    });
});
</script>

Why isn't it loading the jquery file ?

1
  • 1
    I would seriously recommend using one of the CDN's that are available from the Jquery website. Its far faster loading. Also, if you use on site Jquery, put it in a folder out under public_html Commented Jul 29, 2011 at 14:33

1 Answer 1

1

There are several possible problems:

  1. The path to "jquery.js" is relative. Does it exist in the same webserver directory as your HTML page, and is it accessible? You can try to load the script in your browser to see if you get a 404 error.

  2. Are any errors displayed on the Javascript console?

  3. Is jquery.js a clean copy from jquery.com or has it been modified?

An easy way to get around library-related problems is to use one of the CDNs (Content Delivery Networks) to provide the script for you. For example, you can use:

<script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>

This ensures that no errors are introduced into the library locally, allows the script to be used from cache if it was already loaded on another site, and can provide faster results to the user based on the CDN's geographic layout. Please see the "CDN HOSTED JQUERY" section at Downloading jQuery for more information.

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

3 Comments

I just saw that Firebug tells me that it is looking at this url and doesn't find anything there <td>http://tubedjango:8000/tubetest/asset/createAsset/1/jquery-1.6.2.min.js</td> which seems normal. Do I need to provide a url for the jquery file ? What would I put in the related view ? And the file is clean, dl directly from jquery.com
If you choose not to use a CDN, you need to put the full path to the file: src='/tubetest/asset/createAsset/1/jquery-1.6.2.min.js'.
The reason I didn't do that is because this is not a real path, just a url for django so I didn't think it would work. But the CDN is a nice way not to make any mistake

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.