Jump to content
Sign in to follow this  
lewashby

jQuery $(document).ready() not working

Recommended Posts

My browser is loading the webpage just fine and there are no errors reported in /var/log/apache/error.log but I can't get this simple jQuery alert to show up after refreshing the browser.

Do you see anything wrong with me code? Thanks.

 

HTML <-index.html

<!DOCTYPE html>
<html>
    <head>
        <title>jQuery Content Slider</title>
        <link rel='stylesheet' href='css/style.css'>


        <script src='jquery-3.0.0.min.js'></script>
        <script src='js/script.js'></script>
    </head>
 
jQuery <- js/script.js
$(document).ready(function() {
    alert('Doc is ready');
}); 

 

Share this post


Link to post
Share on other sites

JavaScript code is executed by the browser, not the server. So what do the developer tools of your browser say? The Network tab shows HTTP errors (404 etc.), the JavaScript console reports JavaScript errors.

Share this post


Link to post
Share on other sites

I see the 404 error and I also see that is says '$' is not defined. I'm following along with a web dev course on udemy.com and my pages look identical to his so I don't understand what the problem is. I thought $(document).ready() was valid syntax for a .js file?

Share this post


Link to post
Share on other sites

A 404 error means that a file could not be found. So either your jquery-3.0.0.min.js script doesn't exist on the server at all, or you've put it into the wrong location. To match the URL, it must be located next to the index.html file:

src="jquery-3.0.0.min.js"

If you want it to be in your "js" folder instead, you must adjust the URL:

src="js/jquery-3.0.0.min.js"

Share this post


Link to post
Share on other sites

While following along the lessons I forgot I had moved all the files in the previous working lesson into a sub directory 'old' including the jquery file. I feel stupid, and it won't be the last time. Thanks for your help Jacques1. It is solved.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.