Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defer attribute on <script> #70

Open
josephrocca opened this issue Sep 25, 2020 · 0 comments
Open

defer attribute on <script> #70

josephrocca opened this issue Sep 25, 2020 · 0 comments

Comments

@josephrocca
Copy link

If the content of script.js is:

alert('this should happen second');

and the content of our HTML file is:

<script src="/script.js"></script>
<script>alert('this should happen first')</script>

Then it gets "compiled" into this:

<script defer>alert('this should happen second');</script>
<script>alert('this should happen first')</script>

But the defer attribute is invalid for inline scripts, so the code doesn't work the same way.

So, maybe, if the defer attribute is present, the script should be converted into a data URL like so:

<script src="data:text/javascript,alert('this should happen second');" defer></script>
<script>alert('this should happen first')</script>

This seems to be a common workaround when defer is wanted for an inline script. The double quotes within the script would need to be escaped, and multi-line attributes are valid (which is useful for readability), so the output could look something like this:

<script src="data:text/javascript,
    alert(&quot;this should happen second&quot;); 
    // more lines of code...
" defer></script>
<script>alert('this should happen first')</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant