2

When I have test.js file and import the script with:

<script defer="defer" src="/js/test.js"></script>

everything works fine... but if I change the line above in html to:

<script defer="defer">
    $(document).ready(function () {
        $(".overviewPilotPortraitImage").click(function () {
            alert("Hello world!");
        });
    });
</script>

it doesn't work anymore. test.js contains exactly the same script as above, just without script tags.
Yes, I'm sure it's exactly the same.

Head tag with test.js file - WORKING:

<head>
<title>Placeholder - Overview</title>
<meta charset="UTF-8" />

<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="/css/style.css" />
<script defer="defer" src="/js/jquery-1.11.1.min.js"></script>
<script defer="defer" src="/js/bootstrap.min.js"></script>
<script defer="defer" src="/js/test.js"></script>
</head>

Head tag with script in html - NOT WORKING:

<head>
<title>Placeholder - Overview</title>
<meta charset="UTF-8" />

<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="/css/style.css" />
<script defer="defer" src="/js/jquery-1.11.1.min.js"></script>
<script defer="defer" src="/js/bootstrap.min.js"></script>
<script defer="defer">
    $(document).ready(function () {
        $(".overviewPilotPortraitImage").click(function () {
            alert("Hello world!");
        });
    });
</script>
</head>

First, I thought that the script is somehow loaded before the Jquery but it doesn't make sense since the file import works fine. Any ideas?

2
  • 1
    Try removing your defer attribute, it might be preventing the script from running. Commented Jan 10, 2015 at 16:50
  • curious what errors you get, I never use defer Commented Jan 10, 2015 at 16:50

2 Answers 2

6

That's because defer has effect only on external scripts. You can declare the attribuite defer for in-page scripts but it is not considered by the browser.

In your second example, your in-page code is executed immediately, while jquery is loaded after the page has finished loading. More here.

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

2 Comments

That's correct. I tried removing defer attribute from the html script alone but it didn't work. I had to remove defer from all the imports for it to work. Any idea on how to load the scripts during html parsing and executing them after the page is loaded without using defer?
@Sikor Yeah, check out my answer, use window.onload, although I think jQuery's .ready() function should do that for you anyway.
2

Straight from w3schools (and MDN):

Note: The defer attribute is only for external scripts (should only be used if the src attribute is present).

So, basically, you cannot use your defer attribute without the external script.

Perhaps try adding a window.onload:

<script>
window.onload = function(){
    $(document).ready(function () {
        $(".overviewPilotPortraitImage").click(function () {
            alert("Hello world!");
        });
    });
}
</script>

And putting your script tag within the body rather than the head.

<body>
  <!--
    bla - bla - bla
  -->
  <script>
    // tags can go here
  </script>
</body>

3 Comments

It is xhtml so values are required.
Adding window.onload made it work. After quick googling, jquery's .ready is executed as soon as the html ends parsing while window.onload is executed after all content has been loaded. So with window.onload I can keep defer on the other scripts. Thanks.
I was doing some more testing before accepting the answer. So impatient :D

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.