0

My jQuery works fine when it is in an external document but not when I put it in the head of the document. Here's the code:

<head>
        <meta charset="utf-8">
        <meta id="myViewport" name="viewport" content="width=device-width, initial-scale=1.0">
        <title>My title</title>
        <link href="css/style.css" rel="stylesheet">
        <link href="css/responsive_styles.css" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=PT+Serif" rel="stylesheet">
        <script src="https://use.fontawesome.com/7c396dc5cb.js"></script>
        <script>
            $(document).ready(function(){
                var isChrome = !!window.chrome && !!window.chrome.webstore;
                var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;

                if (isChrome){ 
                    $(".welcome-background").css("background-attachment", "scroll");
                    $(".menu-background").css("background-attachment", "scroll");
                    $(".openHours-background").css("background-attachment", "scroll");
                }

                if(iOS){
                    $(".welcome-background").css("background-attachment", "scroll");
                    $(".menu-background").css("background-attachment", "scroll");
                    $(".openHours-background").css("background-attachment", "scroll");
                }
            });   
        </script>
    </head>
2
  • 1
    I can't see the reference to jQuery Commented Apr 20, 2017 at 8:43
  • javascript 101 where is the jquery script? before your code? Commented Apr 20, 2017 at 8:43

2 Answers 2

4

You have not included jQuery library, It should come before jQuery scripts.

<head>
    <meta charset="utf-8">
    <meta id="myViewport" name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My title</title>
    <link href="css/style.css" rel="stylesheet">
    <link href="css/responsive_styles.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=PT+Serif" rel="stylesheet">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://use.fontawesome.com/7c396dc5cb.js"></script>
    <script>
        $(document).ready(function(){
            var isChrome = !!window.chrome && !!window.chrome.webstore;
            var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;

            if (isChrome){ 
                $(".welcome-background").css("background-attachment", "scroll");
                $(".menu-background").css("background-attachment", "scroll");
                $(".openHours-background").css("background-attachment", "scroll");
            }

            if(iOS){
                $(".welcome-background").css("background-attachment", "scroll");
                $(".menu-background").css("background-attachment", "scroll");
                $(".openHours-background").css("background-attachment", "scroll");
            }
        });   
    </script>
</head>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I had the jQuery library at the bottom of the body which worked fine before
0

You need to import your jquery first before your javascript code.

<html>
<head>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  <script>
    $(document).ready(function(){
      alert('Reece your jQUERY is now ready! :)'); // Check if jquery has been loaded.
    });
  </script>
</head>
<body>

</body>
</html>

That's it..Happy Coding! :)

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.