Do script block rendering in safari?

<!DOCTYPE html>
<html>
<head>
<meta charSet="utf-8">
</head>
<body>
  works
  <script defer>
    alert()
  </script>
</body>
</html>

In Chrome and Firefox, this page will display "works" and then show alert box, but in safari 11.1, it will be blank, then show alert box.

Do script block rendering in safari?

1 answer

  • answered 2018-04-17 05:25 Jeff Kranenburg

    The defer element according to the docs say:

    This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed, but before firing DOMContentLoaded.

    It also states that:

    This attribute must not be used if the src attribute is absent (i.e. for inline scripts), in this case it would have no effect.

    Meaning that it wouldn't work with the code you supplied, since a) it is inline and b) it is not a DOM element.

    Although the symptoms seem to be similar with your code, when I used a document.createElement to add items to the DOM in an external file, all browsers functioned the same.

    I would put it down to an anomaly between browser engines. Then again when adding and the DOM and using an external file there was no issue.

    Here is the code, I mentioned the document.createElement before, but that is only part of it, you have to append it to your document as well.

    alert()
    
    let x = document.createElement('p');
    x.innerHTML = "Testing"
    document.body.appendChild(x);