-1
<!doctype html>
<html>
<head>
<title>jQuery install test</title>

<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

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

<style type="text/css"></style>

</head>

<body>

<script>

    if (typeof jQuery != "undefined"){
        alert("jQuery is installed");
    } else {
        alert("jQuery is not installed");
    }

</script>

</body>
</html>

For some reason I am unable to embed jQuery. I named the jQuery file I downloaded jquery.min.js. I also tried changing the src to "http://ajax.googleapis.com/ajax/libs/jquery/latest/jquery.js" but that didn't work as well. Is there a problem with my code? I am new to programming.

mi1ez
  • 1
  • 1
  • this may help you http://stackoverflow.com/questions/7341865/checking-if-jquery-is-loaded-using-javascript – Dil Dilshan Feb 21 '15 at 06:31
  • 1
    The URL with "latest" throws 404. I tried your code using a [valid jQuery URL](https://developers.google.com/speed/libraries/devguide#jquery) and it worked as expected. (Although adding `window.onload` as in the answer is still a good idea). –  Feb 21 '15 at 06:41

2 Answers2

0

Try using window.onload which check after all DOM is loaded

window.onload = function() {
    if (window.jQuery) {  
        alert("jQuery is loaded");
    } else {
        alert("jQuery is not loaded");
    }
}

OR Use

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

Demo

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
0

Things to check, given your HTML above:

1) JQuery file must be in the same folder in your web app as your html file (are you sure you didn't put in the the "scripts" folder - if you've downloaded it into the root of your app you'll need a "/" at the front of the src attribute.

2) Check your console in the browser - see if there's something showing red in there

4) In your browser, go to the code "watch" bit (use chrome for this, simplest browser for doing this sort of thing IMO, tho some people think otherwise) - and add watches for "$" and "jQuery" - see what comes out.

3) Download FIDDLER - this will let you see exactly what's being demanded by the browser - likely you'll see a 404 for the jquery file request. (It's actually a really good tool to get to know outside of this issue - esp if you're new to web dev and will often be scratching your head for a while)

FWIW - the HTML looks broadly fine - it'll be something silly like a wrong path in the "src" or mis-spell int he file name.

HTH

Andrew Hewitt
  • 331
  • 1
  • 6